ios 显示 UIPickerView 文本框被选中,选中后隐藏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26819423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:33:11  来源:igfitidea点击:

Show UIPickerView text field is selected, then hide after selected

iosswiftuipickerview

提问by TimberWebDesign

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.

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

Here is my code:

这是我的代码:

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

}

回答by tbaranes

If I understood well your question, you want:

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

  1. Have an UITextFieldwhich 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
  1. UITextField选择一个显示文本的
  2. 当用户点击时打开一个选择器 UITextField
  3. 当一个项目(在选择器中)被选中时关闭选择器,并将其设置在 UITextField

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

这是管理它的完整代码,您只需链接您的委托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:

我从您的代码中更改的内容:

  • Used UITextFieldDelegateto display the picker when the UITextFieldis selected
  • Hide the picker once an item is selected, and setup the UITextField
  • Set the first row of your picker in the UITextFieldwhen any item is selected
  • 用于UITextFieldDelegate显示选择器中时UITextField被选择
  • 选择项目后隐藏选择器,并设置 UITextField
  • 在选择UITextField任何项目时设置选择器的第一行

回答by Tim Denison

How about in your didSelectRow method you resignFirstResponder?

在你的 didSelectRow 方法中你 resignFirstResponder 怎么样?

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
textfieldBizCat.text = bizCat[row]
pickerBizCat.resignFirstResponder()
}

回答by Tomas Cordoba

Swift 4version

斯威夫特 4版本

override func viewDidLoad() {
    super.viewDidLoad()

    pickerView.dataSource = self
    pickerView.delegate = self

    textField.delegate = self
    textField.inputView = pickerView
}

And the extensions

和扩展

// MARK: - UIPickerViewDelegate

extension ViewController: UITextFieldDelegate {

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        pickerView.isHidden = false
        return false
    }
}

// MARK: - UIPickerViewDelegate

extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return myItems.count
    }

    func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return myItems[row].name
    }

    func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        textField.text = myItems[row].name
        pickerView.isHidden = true
    }
}

回答by innovatorX

  // 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;

        }

    }