UIAlertView不应该调用popViewControllerAnimated

时间:2014-07-15 11:52:31

标签: ios uitableview sdk uialertview uialertviewdelegate

我有一些UITextViews的视图。用户可以输入人员数据,姓名,姓氏,电子邮件等。 编辑完成后,用户点击右上角和视图的“完成” 导航回上一个视图:

- (void)save:(id)sender
{

    [self.view.window endEditing:YES];

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

}

客户要求在某些电子邮件中添加一些验证,例如电子邮件。验证后,UIAlertView会通知数据输入无效,因此不会存储数据。我的问题是AlertView的OK按钮调用“保存”方法和 调用navigationController并调用popViewControllertAnimated。

这里的问题是我想在UIAlertView之后避免自动导航到上一个视图(通过popViewControllerAnimated),更准确地说,我想留在我的编辑视图中并输入一个新的有效电子邮件。

警报视图的代码是

- (void)alertInvalid {

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

    [alertView show];

}

通过-(BOOL)textFieldShouldEndEditing:(UITextField *)textField方法调用。 那么,如何在警报消息后按顺序修改我的代码,使当前的UITextView再次响应?

1 个答案:

答案 0 :(得分:1)

您将需要使用UIAlertViewDelegate。

以下是参考资料:

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

但最重要的是你将实现这个方法:

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

因此,当您实现它时,请检查按钮索引。根据索引,您可以控制逻辑中接下来发生的事情。

当您实例化UIAlertView时,请务必像这样设置委托:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""                                                         
                                                    message:NSLocalizedString(@"res_CurrentPasswordErrorAlert",nil)
                                                   delegate:self // this is the key!
                                          cancelButtonTitle:NSLocalizedString(@"res_OK",nil)
                                          otherButtonTitles:nil];