首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >选择显示UIPickerView文本字段,然后在选择后隐藏

选择显示UIPickerView文本字段,然后在选择后隐藏
EN

Stack Overflow用户
提问于 2014-11-09 00:35:43
回答 2查看 77K关注 0票数 23

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

下面是我的代码:

代码语言:javascript
复制
@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])"

}
EN

回答 2

Stack Overflow用户

发布于 2016-06-29 12:35:19

在你的resignFirstResponder的didSelectRow方法中呢?

代码语言:javascript
复制
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
textfieldBizCat.text = bizCat[row]
pickerBizCat.resignFirstResponder()
}
票数 1
EN

Stack Overflow用户

发布于 2015-09-03 16:27:39

代码语言:javascript
复制
  // 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;

        }

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26819423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档