我试图以编程方式创建UIPickerView并将其显示为文本字段的firstResponder,但是,选择器视图未显示. textField连接到接口构建器中的对象,但是正在以编程方式创建pickerView.
class View: UIViewController { @IBOutlet var picker : UIPickerView = UIPickerView.alloc() @IBOutlet var textField : UITextField = nil override func viewDidLoad() { super.viewDidLoad() picker = UIPickerView() picker.delegate = self picker.dataSource = self picker.backgroundColor = UIColor.blackColor() textField.inputView = picker } } extension View: UIPickerViewDataSource { func numberOfComponentsInPickerView(colorPicker: UIPickerView!) -> Int { return 1 } func pickerView(pickerView: UIPickerView!,numberOfRowsInComponent component: Int) -> Int { return 5 } } extension View: UIPickerViewDelegate { func pickerView(pickerView: UIPickerView!,titleForRow row: Int,forComponent component: Int) -> String! { return "Text" } }
为什么我在运行应用程序时看不到这个pickerView?
编辑:在扩展中添加断点不会停止程序,它们不会被调用.
解决方法
我发现了问题 – 分配输入视图的代码不包括self.它应该读
self.textField.inputView = picker