Evana
Evana

Reputation: 353

UIAlertView displaying keyboard, not able to dismiss it

I have an application that uses UIAlertView in its login Window normally:

self.customAlert = [[IFCustomAlertView alloc] initWithTitle:@"Save Password"
                                                                message:@"¿Do you want the app to remember your password?"
                                                               delegate:self
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:@"Cancel", nil];

The problem is... Since I updated my devices to iOS8 whenever this alertView comes up it shows the keyboard and I can't dismiss it. On iOS7 this doesn't happen.

I am resigning the responders of user and password when the send button is tapped:

-(IBAction)btnSendTapped:(id)sender{
    [self.tfpass resignFirstResponder];
    [self.tfuser resignFirstResponder];
}

I have tried:

[self.view endEditing:YES];

and in some alertViews it does work but in others it doesn't. My AlertViews never have text fields so I think there's no reason for this keyboard to appear.

Also the intro button on the keyboard doesn't hide it, so sometimes the OK and Cancel buttons are obstructed by the keyboard and I can't do nothing on the screen.

I think this may have something to to with the UIAlertView deprecation, but I don't know.

I also have these methods implemented:

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return true;
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    return YES;
}

Any help would be appreciated.

Upvotes: 1

Views: 971

Answers (1)

Popeye Prutya
Popeye Prutya

Reputation: 58

I borrow solution from this blog

For me the keyboard always show up when the alertView.show() has been called.

My solution is using the didPresentALertView method for make sure this will called after the alert view popup. Then we can loop through all UIWindows and it's subviews. I detect it by description name (You can use more accurate method if you want) and just simply remove it from superview.

func didPresentAlertView(alertView: UIAlertView) {
    var tempWindow: UIWindow;
    var keyboard: UIView;

    for var c = 0; c < UIApplication.sharedApplication().windows.count; c++ {
        tempWindow = UIApplication.sharedApplication().windows[c] as! UIWindow
        for var i = 0; i < tempWindow.subviews.count; i++ {
            keyboard = tempWindow.subviews[i] as! UIView

            println(keyboard.description)

            if keyboard.description.hasPrefix("<UIInputSetContainerView") {
                keyboard.removeFromSuperview()
            }
        }
    }
}

Hope this thelp.

Upvotes: 1

Related Questions