[Solved] Picker View uncaught exception SWIFT


The first problem is that you don’t store the created picker view instance. You instantiate it inside of a function, assign the delegate and dataSource and then you don’t store it in your class. So the ARC (Automatic Reference Counting) releases it, because it thinks the instance is not longer needed. Just create a variable in your PickerController and store as long as the view controller is alive.

The second problem is that you actually want to see the picker view, so you need to add it to the view controller’s view. You might need to position it right or use layout constraints (Search for Auto Layout).

var pickerView: UIPickerView!

func createPickerView() {
    pickerView = UIPickerView()
    pickerView.delegate = self
    pickerView.dataSource = self
    view.addSubview(pickerView)
}

2

solved Picker View uncaught exception SWIFT