0

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

- (void)save:(id)sender
{

    [self.view.window endEditing:YES];

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

}

客户要求在某些字段中添加一些验证,例如电子邮件。验证后,UIAlertView 会通知数据输入无效,因此不会存储数据。我的问题是AlertView的OK按钮调用了“save”方法,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 再次响应的警报消息之后修改我的代码?

4

1 回答 1

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];
于 2014-07-15T12:00:41.260 回答