致命错误:在解开uiimageview

时间:2016-12-13 14:56:34

标签: ios swift uiimageview

我有一个带有一个文本字段的下拉图片,uipicker。因此,当用户按下文本字段时 - uipicker将打开以选择值,并且一个完成按钮将用于uipicker。当用户按下uipicker done按钮时,所选数据将被添加到文本字段值。

但我在文本字段的右上角添加了一个图像视图。当用户按下文本字段并打开uipicker时,该uiimageview将上下颠倒。当用户按下完成按钮时,该图像视图将会出现正常位置。

但问题是,我的完成操作按钮是在单独的nsobject类中。我的uiimage视图在一个视图控制器中。我做的是什么?我只是将视图控制器uiimage视图调用到nsobject类,我正在进行图像旋转。

但是当我按下完成按钮时它会崩溃。

这是我在视图控制器中的代码:

    lazy var ctDataPicker = CTDataPickerplayer()
         @IBOutlet weak var sportsDropImage: UIImageView!

            @IBOutlet weak var hobbiesDropImage: UIImageView!

        override func viewDidLoad() {
                super.viewDidLoad()

                ctDataPicker.hobimage? = hobbiesDropImage as UIImageView!
                ctDataPicker.spoimage? = sportsDropImage as UIImageView!



            }

extension CricketFanViewController:UITextFieldDelegate{

    func textFieldDidBeginEditing(textField: UITextField) {


        selectedText = textField

       if  textField == Experience {

        hobbiesDropImage.transform = CGAffineTransformMakeRotation(3.14)
        sportsDropImage.transform = CGAffineTransformMakeRotation(0.0)


            ctDataPicker = CTDataPickerplayer()
            let indexPos = Hobbeies.indexOf(Experience.text!) ?? 0
            ctDataPicker.showPicker(self, inputText: textField, data: Hobbeies,selectedValueIndex: indexPos)
        }
        else if  textField == playedTeam{


        hobbiesDropImage.transform = CGAffineTransformMakeRotation(0.0)
        sportsDropImage.transform = CGAffineTransformMakeRotation(3.14)


            ctDataPicker = CTDataPickerplayer()
            let indexPos = Sports.indexOf(playedTeam.text!) ?? 0
            ctDataPicker.showPicker(self, inputText: textField, data: Sports, selectedValueIndex: indexPos)
        }
    }
}

我的NSObject类代码:

  import Foundation

    import SwiftCountryPicker

    class CTDataPickerplayer: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {

        var dataPicker: UIPickerView!
        var inputText:UITextField!
        var parent:UIViewController!
        var pickerData: [String]!
        var feetStr = String()
        var inchStr = String()


        var hobimage:UIImageView!
        var spoimage:UIImageView!
    func showPicker(parent:UIViewController,inputText:UITextField, data: [String], selectedValueIndex: Int){

        self.inputText = inputText
        self.parent = parent
        self.pickerData = data

        if dataPicker == nil {
            dataPicker = UIPickerView(frame: CGRectMake(0,0,parent.view.frame.size.width, 216))
            dataPicker.backgroundColor = UIColor.whiteColor()
            dataPicker.dataSource = self
            dataPicker.delegate = self
            dataPicker.selectRow(selectedValueIndex, inComponent: 0, animated: true)
            _selectedValue = self.pickerData.count > 0 ? self.pickerData[selectedValueIndex] : ""
        }
        inputText.inputView = dataPicker

        // ToolBar
        let toolBar = UIToolbar()
        toolBar.barStyle = .Default
        toolBar.translucent = true
        toolBar.tintColor = UIColor(hex: "B12420")
        toolBar.backgroundColor = UIColor.whiteColor()
        toolBar.sizeToFit()

        // Adding Button ToolBar
        let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(CTDataPickerplayer.doneClick))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(CTDataPickerplayer.cancelClick))
        toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
        toolBar.userInteractionEnabled = true
        inputText.inputAccessoryView = toolBar

    }


        func doneClick() {


            hobimage.transform = CGAffineTransformMakeRotation(0.0)
            spoimage.transform = CGAffineTransformMakeRotation(0.0)

    }

我的应用程序在完成操作按钮代码中崩溃:

  

hobimage.transform = CGAffineTransformMakeRotation(0.0)               spoimage.transform = CGAffineTransformMakeRotation(0.0)

2 个答案:

答案 0 :(得分:1)

您使hobimagespoimage“隐式解包了选项(类型UIImageView!。)

!运算符视为“crash if nil”运算符。每当你引用一个隐式解包的可选项并且它是nil时,你就会崩溃。

因此,hobimagespoimage可能为零。您可能忘记在“界面”构建器中连接插座。

答案 1 :(得分:0)

问题在于您的ctDataPicker,您已在vc的顶部懒洋洋地初始化,并在imageViews中分配了viewDidLoad。现在,您再次在extension CricketFanViewController:UITextFieldDelegate

中使用相同的对象
            ctDataPicker = CTDataPickerplayer()
            let indexPos = Sports.indexOf(playedTeam.text!) ?? 0
            ctDataPicker.showPicker(self, inputText: textField, data: Sports, selectedValueIndex: indexPos)

请在这里查看并确定您的要求。