简体   繁体   English

用户点击UITextfield时以编程方式实现UIPickerView

[英]Programmatically implementing a UIPickerView when user taps UITextfield

I am currently working on a small project and i have a viewController that has 4 textFields which 3 work ok. 我目前正在开发一个小项目,我有一个viewController有4个textFields,其中3个工作正常。 They take String objects. 它们采用String对象。 However, the 4th textField is supposed to bring up a UIPickerView with 4 selectable items. 但是,第4个textField应该会显示一个带有4个可选项的UIPickerView。

So far this is what i have in my controller that implements this: 到目前为止,这是我在我的控制器中实现的:

     @IBOutlet var pickerTextfield: UITextField!
     @IBOutlet var itemPicker: UIPickerView! = UIPickerView()  

The pickerTextfield is the UITextField object that is the 4th field. pickerTextfield是第4个字段的UITextField对象。 The itemPicker is an unlinked UIPickerView that i want to create programatically. itemPicker是一个未链接的UIPickerView,我想以编程方式创建。

Right below these properties, i have an array of items for the UIPickerView object: 在这些属性的正下方,我有一个UIPickerView对象的项目数组:

var seasonalItems = ["Spring", "Summer", "Fall", "Winter"]

In my viewDidLoad method i have this as follow: 在我的viewDidLoad方法中,我有如下:

    itemPicker.hidden = true;
    pickerTextfield.text = seasonalItems[0]

    pickerTextfield.delegate = self

And the rest of the implementation: 其余的实施:

// Below these lines is the implementation of the Picker //这些行下面是Picker的实现

func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
    return 1
}

// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
    return seasonalItems.count
}

func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
    return seasonalItems[row]
}

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
    pickerTextfield.text = seasonalItems[row]
    itemPicker.hidden = true;
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    itemPicker.hidden = false
    return false
}

So the end result from this is when i tap the pickerTextfield object in the app, it shows the first item of the array (Spring) but in text within the UITextField object but it does not show the UIPickerView object with the other selectable items where i could select one and then hide it when selected. 所以最终的结果就是当我点击应用程序中的pickerTextfield对象时,它显示了数组的第一项(Spring),但是在UITextField对象中的文本中,但它没有显示UIPickerView对象与其他可选项目在哪里我可以选择一个,然后在选中时隐藏它。

My question is, where or what am i doing wrong here? 我的问题是,我在哪里或者我做错了什么? i been trying to figure this out on my own but i do not seem to get good clear examples with Swift and storyboards. 我一直试图自己解决这个问题,但我似乎没有得到Swift和故事板的清晰例子。 I much rather not drag a UIPickerView in the storyboard but rather the way i attempted to implement. 我更倾向于不在故事板中拖动UIPickerView,而是我试图实现的方式。 Thanks 谢谢

You can give UIPickerView as inputView for your TextField in which you want to show picker view. 您可以将UIPickerView作为您要显示选择器视图的TextField inputView You also do not need to initially hide picker view in this case. 在这种情况下,您也不需要最初隐藏选择器视图。

pickerTextfield.inputView = itemPicker

When you use UIPickerView as inputView of any UITextField then when you tap on the TextField instead of default keypad PickerView will show. 当您使用UIPickerView作为任何UITextField inputView ,当您点击TextField而不是默认键盘PickerView将显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM