首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调用UIAlertView后停止代码执行

调用UIAlertView后停止代码执行
EN

Stack Overflow用户
提问于 2012-08-26 19:23:54
回答 2查看 2.6K关注 0票数 2

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-27 21:22:31

最终使用的是:

..。

代码语言:javascript
复制
[alert show];

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

    }
票数 7
EN

Stack Overflow用户

发布于 2012-08-26 23:12:58

看起来您并不想执行在alertview show方法之后编写的代码。要实现这一点,请将这些代码行添加到一个方法中,并在下面的UIAlertView委托中调用该方法。

代码语言:javascript
复制
 - (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和tag设置来完成此操作。

如果您有三个警报,请在类的顶部在@接口之前声明一个NSEnum,如下所示:

代码语言:javascript
复制
 // alert codes for alertViewDelegate // AZ 09222014
 typedef NS_ENUM(NSInteger, AlertTypes)
{
    UserConfirmationAlert = 1, // these are all the names of each alert
    BadURLAlert,
    InvalidChoiceAlert
}; 

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

代码语言:javascript
复制
myAlert.tag = UserConfirmationAlert;

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

代码语言:javascript
复制
// 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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12129703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档