Issue with tap over keyboard's return key for UIAlertView's UIAlertViewStyleSecureTextInput field iOS 8

257 Views Asked by At

While tapping over keyboard's return key while entering value in UIAlertViewStyleSecureTextInput of a UIAlertView is triggering call to alertView:clickedButtonAtIndex: UIAlertViewDelegate in iOS 8

In iOS 7 same UIAlertViewDelegate call wasn't getting triggered in same conditions.

I am not sure if this is an issue in iOS 8 or iOS 7. But my code stopped functioning as per expectation iOS 8.

Code sample:

    #include "MPAMyViewController.h"

    static NSString *MPAPasswordConfirmationAlertTitle = @"Please confirm your password.";
    static NSString *MPAPasswordConfirmationAlertCancelButtonTitle = @"Cancel";
    static NSString *MPAPasswordConfirmationAlertOkButtonTitle = @"OK";
    static NSString *MPAPasswordConfirmationTextFieldPlaceholder = @"Enter Password";

    @interface MPAMyViewController () <UIAlertViewDelegate, UITextFieldDelegate>

    @property (strong, nonatomic) NSString *errorFromRequest;
    @property (strong, nonatomic) UIAlertView *passwordAlertView;

    - (void)processRequest:(UITextField *)passwordTextField;

    @end

    @implementation MPAMyViewController

    - (IBAction)showPasswordConfirmationAlert:(id)sender {
        self.passwordAlertView = [[UIAlertView alloc] initWithTitle:MPAPasswordConfirmationAlertTitle
                                                            message:self.errorFromRequest
                                                           delegate:self
                                                  cancelButtonTitle:MPAPasswordConfirmationAlertCancelButtonTitle
                                                  otherButtonTitles:MPAPasswordConfirmationAlertOkButtonTitle, nil];

        self.passwordAlertView.alertViewStyle = UIAlertViewStyleSecureTextInput;

        UITextField *passwordTextField = [self.passwordAlertView textFieldAtIndex:0];
        [passwordTextField becomeFirstResponder];
        passwordTextField.placeholder = MPAPasswordConfirmationTextFieldPlaceholder;
        passwordTextField.delegate = self;

        [self.passwordAlertView show];
    }


    #pragma mark - UIAlertViewDelegate

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

        if([title isEqualToString:MPAPasswordConfirmationAlertOkButtonTitle]) {
            UITextField *passwordTextField = [alertView textFieldAtIndex:0];
            [self processRequest:passwordTextField];
        }
    }


    #pragma mark - TextField Delegate

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [self.passwordAlertView dismissWithClickedButtonIndex:self.passwordAlertView.firstOtherButtonIndex animated:YES];

        return YES;
    }

    - (void)processRequest:(UITextField *)passwordTextField {
        NSString *password = passwordTextField.text;

        // CODE: send request
    }

    @end
4

There are 4 best solutions below

0
LoVo On

I had a similar problem with an UIAlertView Delegatemethod not getting called. I found out that UIAlertView is deprecated in iOS 8. You should use UIAlertController.

1
Deepak Thakur On

Use UIAlertController to make it compatible with iOS 8.

1
Fatti Khan On

Using alertView , use the delegation and use any of the Delegate method of the UIAlertView .. You haven't set the delegate to you current view controller so try the code below

use the

 UIAlertView * alertView  = UIAlertView()
  viewDidLoad(){

 alertView.delegate = self
}

Using the above code you delegate methods will be invoked.

0
Neha On

Try below snippet of code.

#pragma mark - TextField Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {  
    return NO;
}

Note: If you are using above delegate for other text fields then write conditional code as per your requirement.