调用UIAlertView后停止执行代码

时间:2012-08-26 11:23:54

标签: iphone

在用户按下OK按钮之后,如何在显示UIAlertView后停止代码执行?如果这是一个问题,那么解决方法是什么?

2 个答案:

答案 0 :(得分:7)

结束使用:

...

[alert show];

while ((!alert.hidden) && (alert.superview != nil))
    {
        [[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];

    }

答案 1 :(得分:1)

您似乎不想执行刚刚[alertview show]方法之后编写的代码。为了实现这一点,将这些代码行添加到方法中,并在UIAlertView的以下委托中调用该方法。

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if(buttonIndex == OKButtonIndex)
       {
         // Issue a call to a method you have created that encapsulates the 
         // code you want to execute upon a successful tap.
         // [self thingsToDoUponAlertConfirmation];
       }

    }

现在,如果您要在班级中安装多个UIAlertView,您需要确保可以轻松处理每个UIAlertView。您可以使用UIAlertView上的NSEnum和标记设置来执行此操作。

如果您有三个警报,则在您的类顶部声明一个NSEnum,然后在@interface之前声明:

 // alert codes for alertViewDelegate // AZ 09222014
 typedef NS_ENUM(NSInteger, AlertTypes)
{
    UserConfirmationAlert = 1, // these are all the names of each alert
    BadURLAlert,
    InvalidChoiceAlert
}; 

然后,在[提示显示]之前,将警报的标记设置为显示。

myAlert.tag = UserConfirmationAlert;

然后在您的UIAlertDelegate中,您可以在开关/案例中执行所需的方法,如下所示:

// Alert handling code
#pragma mark - UIAlertViewDelegate - What to do when a dialog is dismissed.
// We are only handling one button alerts here.  Add a check for the buttonIndex == 1
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (alertView.tag) {
        case UserConfirmationAlert:
            [self UserConfirmationAlertSuccessPart2];
            alertView.tag = 0;
            break;

        case BadURLAlert:
            [self BadURLAlertAlertSuccessPart2];
            alertView.tag = 0;
            break;

        case InvalidChoiceAlert:
            [self InvalidChoiceAlertAlertSuccessPart2];
            alertView.tag = 0;
            break;

        default:
            NSLog(@"No tag identifier set for the alert which was trapped.");
            break;
    }
}
相关问题