您好,当用户单击textfield时,我正试图以编程方式弹出一个UIPickerView
视图。我试过了,但没用。它不起作用。当我单击文本字段时什么都没有发生
class UserProfileTableViewController: UITableViewController,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource {
var itemPicker: UIPickerView! = UIPickerView()
@IBOutlet weak var genderTxtField: UITextField!
var gender = ["MALE","FEMALE"]
override func viewDidLoad() {
super.viewDidLoad()
genderTxtField.delegate = self
itemPicker!.delegate = self
itemPicker!.dataSource = self
itemPicker!.backgroundColor = UIColor.blackColor()
self.genderTxtField.inputView = itemPicker
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return gender.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gender[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
genderTxtField.text = gender[row]
//.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
itemPicker.hidden = false
return false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最佳答案
当前代码中的问题在于此方法:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
itemPicker.hidden = false
return false // <--- this is not letting the textField become editable
}
我建议完全取消这种方法。
关于ios - UITEXTFIELD InputView Uipicker无法快速运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35057684/