简体   繁体   English

在使用2个不同的ViewController时解开Optional值时,自定义类抛出错误意外地发现为nil

[英]Custom Class throwing error unexpectedly found nil while unwrapping an Optional value when using 2 different ViewControllers

I'm still new to swift so any help would be appreciated. 我仍然是新手,所以我们将不胜感激。

I have a simple set of pickerviews which are defined in 2 custom classes (ImageModelPicker and TimeModelPicker. When ever I load the ViewController that they are in directly there is no issue. When I load them after loading another (title screen) view controller their values throw: found nil while unwrapping an Optional value when using 2 different ViewControllers 我有一组简单的pickerviews,它们在2个自定义类(ImageModelPicker和TimeModelPicker)中定义。每当我加载直接位于它们的ViewController时,都没有问题。当我在加载另一个(标题屏幕)视图控制器之后加载它们时,它们的值抛出:使用两个不同的ViewController时解开一个可选值时发现nil

Here is the main code: 这是主要代码:

@IBOutlet weak var pickerImageView: UIPickerView! // image pickerview
var imageModelPicker: ImageModelPicker! // initializing

@IBOutlet weak var pickerTimeView: UIPickerView! //time pickerview
var timeModelPicker: TimeModelPicker! // initializing

override func viewDidLoad() {
    super.viewDidLoad()

    // image model picker
    self.imageModelPicker = ImageModelPicker()
    self.imageModelPicker.modelData = Data.getData()
    //rotate frame and delegate pickerImageView
    let rotationAngle = -90 * (Double.pi/180)
    self.pickerImageView.delegate = imageModelPicker
    self.pickerImageView.dataSource = imageModelPicker
    pickerImageView.transform = CGAffineTransform(rotationAngle: CGFloat(rotationAngle))
    pickerImageView.frame = CGRect(x: -100, y: (view.center.y) - 25, width: view.frame.width + 200, height: 50)
    pickerImageView.selectRow(5, inComponent: 0, animated: true) // select default value

Actually, you are not initialize a variable by this code: 实际上,您不是通过以下代码初始化变量:

var imageModelPicker: ImageModelPicker!

As a result, you get an error, because you try to use nil instead of instance of the ImageModelPicker class. 结果,您得到一个错误,因为您尝试使用nil代替ImageModelPicker类的实例。

Try this code: 试试这个代码:

@IBOutlet weak var pickerImageView: UIPickerView! // image pickerview
var imageModelPicker = ImageModelPicker() // initializing

@IBOutlet weak var pickerTimeView: UIPickerView! //time pickerview
var timeModelPicker: TimeModelPicker! // initializing

override func viewDidLoad() {
    super.viewDidLoad()

    // image model picker
    self.imageModelPicker.modelData = Data.getData()
    //rotate frame and delegate pickerImageView
    let rotationAngle = -90 * (Double.pi/180)
    self.pickerImageView.delegate = imageModelPicker
    self.pickerImageView.dataSource = imageModelPicker
    pickerImageView.transform = CGAffineTransform(rotationAngle: CGFloat(rotationAngle))
    pickerImageView.frame = CGRect(x: -100, y: (view.center.y) - 25, width: view.frame.width + 200, height: 50)
    pickerImageView.selectRow(5, inComponent: 0, animated: true) // select default value

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将视图控制器分配给选项卡栏时,“致命错误:在展开可选值时意外发现零” - “fatal error: unexpectedly found nil while unwrapping an Optional value” while assigning viewcontrollers to tabbar 使用NSFileManager检索多个值时收到“致命错误:在展开可选值时意外发现nil” - “fatal error: unexpectedly found nil while unwrapping an Optional value” received when using NSFileManager to retrieve multiple values 致命错误:在Swift 2中使用AudioPlayer时,在展开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value when using AudioPlayer in Swift 2 NSString到String引发“致命错误:在展开可选值时意外发现nil” - NSString to String throwing “fatal error: unexpectedly found nil while unwrapping an Optional value” 此代码段引发“致命错误:在展开可选值时意外发现nil”。 我尝试使用“ if let” - This bit of code is throwing “fatal error: unexpectedly found nil while unwrapping an Optional value”. I have tried using “ if let” 标头自定义视图:在展开可选值时意外发现nil - Header Custom View: unexpectedly found nil while unwrapping an Optional value 展开可选值时意外找到nil - Unexpectedly found nil while unwrapping an Optional value 解包 Optional 值时意外发现 nil - unexpectedly found nil while unwrapping an Optional value 展开Optional值时意外发现nil - unexpectedly found nil while unwrapping an Optional value 展开可选的value()时意外找到nil - Unexpectedly found nil while unwrapping an Optional value()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM