显示UIPickerView文本字段已选中,然后在选中后隐藏 [英] Show UIPickerView text field is selected, then hide after selected

查看:103
本文介绍了显示UIPickerView文本字段已选中,然后在选中后隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个文本框,当它被选中时,UIPickerView会打开并提供可供选择的选项。选择后,UIPickerView将隐藏,所选项目将显示在文本框中。我尝试了在网上找到的不同代码,但我无法让它工作。如果有人可以为此建议一个完整的代码,或者告诉我在我的代码中我做错了什么,这将是非常棒的。非常感谢。

I am trying to create a text box that when it is selected a UIPickerView opens up with choices to select from. Once selected, the UIPickerView hides and the selected item is displayed in the text box. I tried different pieces of code I found online but I just can't get it to work. If someone can suggest a complete code for this or tell me what I am doing wrong in my code, that would be super awesome. Thanks so much.

这是我的代码:

@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])"

}


推荐答案

如果我理解你的问题,你想要:

If I understood well your question, you want:


  1. 拥有 UITextField 显示选中的文本

  2. 当用户点击 UITextField 时打开一个选择器

  3. 选择项目(在选择器中)时关闭选择器,并将其设置在`UITextField

  1. Have an UITextField which display a text selected
  2. Opening a picker when the user click on the UITextField
  3. Close the picker when an item (in the picker) is selected, and set it in the `UITextField

这是管理它的完整代码,你只需要链接 UITextField 的代表:

This is the complete code to manage it, you just have to link the delegate of your UITextField:

@IBOutlet var textfieldBizCat: UITextField!
@IBOutlet var pickerBizCat: UIPickerView! = UIPickerView()

var bizCat = ["Cat One", "Cat Two", "Cat Three"]


override func viewDidLoad() {
    super.viewDidLoad()
    pickerBizCat.hidden = true;
    textfieldBizCat.text = bizCat[0]
}

// 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]
    pickerBizCat.hidden = true;
}

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

我从您的代码中更改了什么:

What I changed from your code:


  • 使用 UITextFieldDelegate 显示选择器时选择 UITextField

  • 选择项目后隐藏选择器,并设置 UITextField

  • 选择任何项目时,在 UITextField 中设置选择器的第一行

  • Used UITextFieldDelegate to display the picker when the UITextField is selected
  • Hide the picker once an item is selected, and setup the UITextField
  • Set the first row of your picker in the UITextField when any item is selected

这篇关于显示UIPickerView文本字段已选中,然后在选中后隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆