我正在尝试创建一个文本框,当它被选中时,将打开一个UIPickerView,其中包含可供选择的选项。选择后,UIPickerView将隐藏,而所选项目将显示在文本框中。我尝试了我在网上找到的不同代码片段,但我就是不能让它工作。如果有人能为此建议一个完整的代码,或者告诉我我在代码中做错了什么,那就太棒了。非常感谢。
下面是我的代码:
@IBOutlet var textfieldBizCat: UITextField!
@IBOutlet var pickerBizCat: UIPickerView! = UIPickerView()
var bizCat = ["Cat One", "Cat Two", "Cat Three"]
override func viewDidLoad() {
super.viewDidLoad()
var bizCatCount = bizCat.count
self.textfieldBizCat.inputView = pickerView
}
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return bizCat.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return bizCat[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
textfieldBizCat.text = "\(bizCat[row])"
}
发布于 2016-06-29 12:35:19
在你的resignFirstResponder的didSelectRow方法中呢?
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
textfieldBizCat.text = bizCat[row]
pickerBizCat.resignFirstResponder()
}
发布于 2015-09-03 16:27:39
// pressing the button again would hide the uipickerview. when pressed the first time, update the button's label to "done" , "hide" or whatever suits u!
@IBAction func propertyTypeButtonPressed(sender: UIButton)/* the name of your button's action*/
{
count++; //declare it first
ViewContainigPickerView.hidden = false
self.view.endEditing(true)
if (count == 2)
{
ViewContainingPickerView.hidden = true /* if you placed your picker on a separate view for simplicity*/
count = 0;
}
}
https://stackoverflow.com/questions/26819423
复制相似问题