0

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

}
5
  • Put this on your button click action: yourtextField.resignFirstResponder() Commented Jun 18, 2016 at 7:53
  • Not working, it goes synchronous. Commented Jun 23, 2016 at 8:24
  • You have to add this line on button click. Commented Jun 23, 2016 at 9:37
  • Yes, I've added it in between @IBAction line and dispatch_async line, Commented Jun 23, 2016 at 11:03
  • update your question with new code Commented Jun 23, 2016 at 11:07

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Thanks for your suggestion, as above answer, it's not working, it goes synchronous again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.