Darko
Darko

Reputation: 615

Hide keyboard when button is pressed

I'm trying to hide my keyboard when a button is pressed. I'm using GCD to simultaneously fetch NSData from a server and show an animation.

The problem is, whenever I press the button the keyboard stays active. I've tried putting the _self.view.endEditing(true) into the GCD function but with no success.

I would greatly appreciate if you would help me with this.

 @IBAction func tragi(sender: UIButton) {



    dispatch_async(dispatch_get_main_queue()) { [weak self] in
        if let _self = self {

             SwiftSpinner.show("Fetching data.....")


            _self.parseJSON2 { (makeModel) in
                print("print this")
            }
        }}

}

Upvotes: 0

Views: 2226

Answers (2)

Abhinav Jha
Abhinav Jha

Reputation: 345

Please try self.view.endEditing(true)

The above function causes the view (or one of its embedded text fields) to resign the first responder status.

This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to true, the text field is never even asked; it is forced to resign.

as per Apple Docs.

Upvotes: 1

Savchenko Dmitry
Savchenko Dmitry

Reputation: 360

Try make call resignFirstResponder for active object in your button action and in main queue : someTextField.resignFirstResponder()

Upvotes: 0

Related Questions