Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - hide pickerView after value selected

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

}
like image 585
Greg Avatar asked May 28 '15 01:05

Greg


2 Answers

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

}
like image 146
Bannings Avatar answered Oct 13 '22 15:10

Bannings


One way to hide picker view is:

self.view.endEditing(true) 

This worked for me.

like image 37
Anas Avatar answered Oct 13 '22 16:10

Anas