0

I have a view with some UITextViews. The user can enter person data, name, surname, email etc. When the editing is finished the user hits "Done" on the top right corner and the view navigates back to a the previous view like this:

- (void)save:(id)sender
{

    [self.view.window endEditing:YES];

    if (self.data ...)
    {
        [self updateUser];
        [self.navigationController popViewControllerAnimated:YES];
    }

}

The customer requested to add some validation at the some fiels, eg email. After the validation a UIAlertView informs that the data input was not valid, therefore the data are not stored. My problem is that the OK button of the AlertView calls the "save" method and the navigationController is called and the popViewControllertAnimated is called.

The problem here is that I would like after the UIAlertView to avoid the auto navigation to the previous view (via popViewControllerAnimated) and more precisely I would like to stay on my edit view and type a new valid email.

The code for the alert view is

- (void)alertInvalid {

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""                                                                      message:NSLocalizedString(@"res_CurrentPasswordErrorAlert",nil)
                              delegate:nil cancelButtonTitle: NSLocalizedString(@"res_OK",nil) otherButtonTitles:nil];

    [alertView show];

}

which is called via the -(BOOL)textFieldShouldEndEditing:(UITextField *)textField method. So, how do I modify my code in order after the alert message the current UITextView to become responsive again?

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
cateof
  • 6,608
  • 25
  • 79
  • 153

1 Answers1

1

You will want to make use of UIAlertViewDelegate.

Here is the reference:

https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html

But the bottom line is that you will implement this method:

  • (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

So when you implement it, check the button index. Based on the index, you can control what happens next in your logic.

When you instantiate your UIAlertView, be sure to set the delegate like this:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""                                                         
                                                    message:NSLocalizedString(@"res_CurrentPasswordErrorAlert",nil)
                                                   delegate:self // this is the key!
                                          cancelButtonTitle:NSLocalizedString(@"res_OK",nil)
                                          otherButtonTitles:nil];
joelg
  • 1,094
  • 9
  • 19
  • What code do we need in the clickedButtonAtIndex? I just want to stay on the same view with the UITextView first responder. – cateof Jul 15 '14 at 12:55
  • The code you need in clickedButtonAtIndex totally depends on what you want to do. You could do a switch on the buttonIndex parameter and then code your logic accordingly. – joelg Jul 15 '14 at 16:08