iosswiftkeyboardalertresignfirstresponder

While displaying an alert in IOS, keyboard does not resign from View


While displaying an alert(Wrong password) in IOS 8, keyboard opens automatically and hide the alert(just in Iphone 4s because of the screen's size), so I can't click in "OK" and I also can't dismiss keyboard because first I need to close the alert!

Keyboard hides alert

(It seems the app is recovering last keyboard's state and showing up again)

How can I close the keyboard before calling the alert?(this way the state will be "closed")

I've tried:

myTextField!.resignFirstResponder()

While calling the button, but it didn't work, the alert shows up and automatically the keyboard opens over it !


Solution

  • if myTextField!.resignFirstResponder() is not working properly try this when you present the alert before call this -->self.view.endEditing(true)

    the above function is not work well , try

    Choice -1 :Using the Responder Chain

    UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
    

    This will resign the first responder (and dismiss the keyboard) every time, without you needing to send resignFirstResponder to the proper view. No matter what, this will dismiss the keyboard. It’s by far the best way to do it: no worrying about who the first responder is

    Choice -2 :UIView’s endEditing

    (assuming your text field is a subview of the view you call this on). Most of the time:

    self.view.endEditing(true)