我是快速编程的新手,我想将 UIPickerView 设置为 UITextField 的输入。这是我的代码:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet var selectTransportCompany: UITextField!
var data = ["DHL Express", "Fed Ex", "TNT", "Express Mail"]
var picker = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
selectTransportCompany.inputView = picker
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return data.count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectTransportCompany.text = data[row]
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return data[row]
}
}
但是,当我运行此代码时,出现错误提示
线程1:EXC_BAD_INSTRUCTION
进入调试器,我得到以下信息:
致命错误:解开Optional值时意外发现nil
有人知道这个问题是什么,我也关注this tutorial。
最佳答案
您只需要将视图控制器文本字段出口连接到故事板selectTransportCompany textField。
关于ios - 快速将UIPickerView设置为UITextField的输入时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34367864/