2

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 !

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
FelipeF
  • 23
  • 1
  • 3

5 Answers5

2

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)
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • in my choice use **UIAlertController** – Anbu.Karthik Nov 12 '15 at 11:54
  • I’m trying the UIAlertController and I think it’s gonna work but how can I call the UIAlertController from a NSObject? " self.presentViewController(alertController, animated: true, completion:nil) " doesn’t work !! error message says “ alerta does not have a member named “presentViewController" – FelipeF Nov 12 '15 at 13:12
  • set like UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alerta, animated: true, completion: nil) – Anbu.Karthik Nov 12 '15 at 13:16
  • @FelipeF -- this is another issue , if moderator see this delete the answer and question also, ok lets we try check this link http://stackoverflow.com/questions/24223427/assign-a-value-to-a-controller-property-in-swift – Anbu.Karthik Nov 12 '15 at 13:33
  • @FelipeF -- try once in previous based use UIalertview and try my answer , repley me you get the answer or not, for get UIalertviewcontroller, we will go on next step – Anbu.Karthik Nov 12 '15 at 13:36
  • @FelipeF - I am waiting for your reply – Anbu.Karthik Nov 12 '15 at 13:38
  • Well, basically when I try to use the code UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewCont‌​roller(alerta, animated: true, completion: nil) Error: ( UIViewController does not have a member named ‘presentViewController’ ) and when I try: self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) Error: (Alerta does not have a member named ‘window') I’m pretty sure it’s about inherits ! I’m trying to fix it and also looking some exemples in the stackoverflow. Anyway I’m sure UIAlertController is the best way! – FelipeF Nov 12 '15 at 16:01
  • @FelipeF -- UIAlertController is the replacement of UIAlertview, if my answer is useful give the upvote for teh answer,it is useful for future – Anbu.Karthik Nov 12 '15 at 16:03
0

set Delegate to myTextField

Include

func textFieldShouldReturn(textField: UITextField) -> Bool
    {
        textField .resignFirstResponder()
        return true
    }

Other wise Try the following

var activeField : UITextField!

    func textFieldDidBeginEditing(textField: UITextField)
    {
        activeField = textField
    }
    func textFieldDidEndEditing(textField: UITextField)
    {
        activeField = nil
    }
    func textFieldShouldReturn(textField: UITextField) -> Bool
    {
        textField .resignFirstResponder()
        return true
    }

Call activeField.resignFirstResponder() before alert appears

Velu Loganathan
  • 241
  • 2
  • 11
0

I think from iOS8 you need to use UIAlertController instead of UIAlertView. Using UiAlertView in iOS8 and above is causing keyboard to popup unnecessarily. I have seen this and i made a condition to use UIAlertController for iOS8 and above. In below version UIAlertView should work fine

harsha yarabarla
  • 506
  • 4
  • 11
  • I’m trying the UIAlertController and I think it’s gonna work but how can I call the UIAlertController from a NSObject? self.presentViewController(alertController, animated: true, completion:nil) The line “ self.presentViewController(alertController, animated: true, completion:nil) “ doesn’t work !! error message says “ alerta does not have a member named “presentViewController" – FelipeF Nov 12 '15 at 13:11
  • self.window?.rootViewController?.presentViewController(importAlert, animated: true, completion: nil) try this – harsha yarabarla Nov 12 '15 at 13:17
  • Does not have a member named "window" – FelipeF Nov 12 '15 at 13:25
  • do you have any git link ? btw your view controller inherits from UiViewController right ? – harsha yarabarla Nov 12 '15 at 13:30
  • Well, basically when I try to use the code UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewCont‌​‌​roller(alerta, animated: true, completion: nil) Error: ( UIViewController does not have a member named ‘presentViewController’ ) and when I try: self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) Error: (Alerta does not have a member named ‘window') I’m pretty sure it’s about inherits ! I’m trying to fix it and also looking some exemples in the stackoverflow. Anyway I’m sure UIAlertController is the best way! – FelipeF Nov 12 '15 at 16:08
0

This is UIAlertView bug on iOS 8. I have same problem but UIAlertController has not problem. :3

UIAlertView was deprecated since iOS8.

https://developer.apple.com/reference/uikit/uialertview

0

In Swift 4 : below code worked for me

    DispatchQueue.main.async() {
        self.view.endEditing(true)
    }
Akhil Reddy
  • 183
  • 1
  • 1
  • 5