Swift - hide pickerView after value selected

独自空忆成欢 提交于 2019-12-07 03:39:56

问题


I have found similar questions about this and their answers were helpful but I am stuck at one last thing. I am trying to get a pickerView to appear when I tap on a field, then when data is selected, I want the pickerView to hide. I am able to get the data from the pickerView to hide, however, there is still a grey rectangle that was behind the pickerView that remains. If I tap on the screen, not on a field or the pickerView, the gray rectangle hides, then reappears when I tap on the next field, only to remain after the new data is chosen, until I tap on some blank screen.

I am including my code below and you will see that I have tried several options. These are not all I have tried but after several attempts, I began to just comment out instead of delete so I could remember what I had done. I am very new to coding so I appreciate this site as a resource and thank you for your assistance.

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

}

回答1:


If you want to hide the pickerView, try this:

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))

}



回答2:


One way to hide picker view is:

self.view.endEditing(true) 

This worked for me.




回答3:


i tried resigning the first responder of the text field in 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()
}

}



来源:https://stackoverflow.com/questions/30495585/swift-hide-pickerview-after-value-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!