使用 2 个不同的 ViewController 展开可选值时,自定义 Class 抛出错误意外发现 nil
Custom Class throwing error unexpectedly found nil while unwrapping an Optional value when using 2 different ViewControllers
我还是 swift 的新手,所以我们将不胜感激。
我有一组简单的 pickerviews,它们在 2 个自定义 类(ImageModelPicker 和 TimeModelPicker)中定义。每当我直接加载它们所在的 ViewController 时,都没有问题。当我在加载另一个(标题屏幕)视图控制器后加载它们它们的值抛出:在使用 2 个不同的 ViewControllers
时展开可选值时发现 nil
主要代码如下:
@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
实际上,您并不是通过以下代码初始化变量:
var imageModelPicker: ImageModelPicker!
结果,您收到一个错误,因为您尝试使用 nil 而不是 ImageModelPicker 的实例 class。
试试这个代码:
@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
我还是 swift 的新手,所以我们将不胜感激。
我有一组简单的 pickerviews,它们在 2 个自定义 类(ImageModelPicker 和 TimeModelPicker)中定义。每当我直接加载它们所在的 ViewController 时,都没有问题。当我在加载另一个(标题屏幕)视图控制器后加载它们它们的值抛出:在使用 2 个不同的 ViewControllers
时展开可选值时发现 nil主要代码如下:
@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
实际上,您并不是通过以下代码初始化变量:
var imageModelPicker: ImageModelPicker!
结果,您收到一个错误,因为您尝试使用 nil 而不是 ImageModelPicker 的实例 class。
试试这个代码:
@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