Swift - 在选中值后隐藏pickerView

时间:2015-05-28 01:07:09

标签: ios swift uipickerview

我发现了类似的问题,他们的答案很有帮助,但我最后还是坚持了。当我点击一个字段时,我试图让pickerView出现,然后当选择数据时,我希望pickerView隐藏。我能够从pickerView中获取数据以隐藏,但是仍然存在一个灰色矩形,它位于pickerView后面。如果我点击屏幕,而不是在字段或pickerView上,灰色矩形会隐藏,然后当我点击下一个字段时再次出现,只有在选择新数据后才会保留,直到我点击一些空白屏幕。

我在下面包含我的代码,你会看到我尝试了几个选项。这些都不是我尝试过的,但经过多次尝试,我开始只是注释而不是删除,所以我记得我做了什么。我对编码很陌生,所以我很欣赏这个网站作为资源,感谢您的帮助。

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

    @IBOutlet var enterSeason: UITextField!

    @IBOutlet var enterSport: UITextField!

    var dataPickerView = UIPickerView()    
    var season = ["2013", "2014", "2015"] //multi-season
    var sport = ["Baseball", "Football", "Basketball", "Hockey"]
    var activeDataArray = []

    override func viewDidLoad() {
        super.viewDidLoad()

        enterSeason.inputView = dataPickerView
        enterSport.inputView = dataPickerView
        dataPickerView.delegate = self
        dataPickerView.dataSource = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func textFieldDidBeginEditing(textField: UITextField) {
        activeDataArray = [] //clear out the clicked field data array
        if textField == enterSeason {
            activeDataArray = season
        } else
            if textField == enterSport {
                activeDataArray = sport
            }
        dataPickerView.reloadAllComponents()
        dataPickerView.hidden = false
    }

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

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

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

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if activeDataArray == season {
            enterSeason.text = season[row] as String
        }
        else if activeDataArray == sport {
            enterSport.text = sport[row] as String
        }
        //trying to hide the dataPicker
        dataPickerView.hidden = true
        //dataPickerView.reloadAllComponents()
        //self.dataPickerView.resignFirstResponder()
        //self.dataPickerView.frameForAlignmentRect(CGRectMake(0, 900, 375, 219))

    }

    //function to hide data in
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    //funtion to hide keyboard when screen is tapped
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.view.endEditing(true)
    }

}

3 个答案:

答案 0 :(得分:15)

如果你想隐藏pickerView,试试这个:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if activeDataArray == season {
        enterSeason.text = season[row] as String
    }
    else if activeDataArray == sport {
        enterSport.text = sport[row] as String
    }
    //trying to hide the dataPicker
    self.view.endEditing(true)
    //dataPickerView.reloadAllComponents()
    //self.dataPickerView.resignFirstResponder()
    //self.dataPickerView.frameForAlignmentRect(CGRectMake(0, 900, 375, 219))

}

答案 1 :(得分:1)

隐藏选择器视图的一种方法是:

self.view.endEditing(true) 

这对我有用。

答案 2 :(得分:0)

我尝试在didSelectRow

中重新调整文本字段的第一个响应者
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if activeDataArray == season {
    enterSeason.text = season[row] as String
    enterSeason.resignFirstResponder()
}
else if activeDataArray == sport {
    enterSport.text = sport[row] as String
    enterSport.resignFirstResponder()
}

}