显示UIPickerView文本字段已选中,然后在选中后隐藏

时间:2014-11-08 16:35:43

标签: ios swift uipickerview

我正在尝试创建一个文本框,当它被选中时,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])"

}

4 个答案:

答案 0 :(得分:44)

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

  1. UITextField显示选中的文字
  2. 当用户点击UITextField
  3. 时打开选择器
  4. 选择项目(在选择器中)时关闭选取器,并将其设置在`UITextField
  5. 这是管理它的完整代码,您只需要链接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
    }
    

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

    • 使用UITextFieldDelegate在选择UITextField时显示选择器
    • 选择项目后隐藏选择器,然后设置UITextField
    • 选择任何项目时,在UITextField中设置选择器的第一行

答案 1 :(得分:0)

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

        }

    }

答案 2 :(得分:0)

你的didSelectRow方法怎么样你resignFirstResponder?

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

答案 3 :(得分:0)

Swift 4 版本

override func viewDidLoad() {
    super.viewDidLoad()

    pickerView.dataSource = self
    pickerView.delegate = self

    textField.delegate = self
    textField.inputView = pickerView
}

扩展名

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