2012-08-26 119 views
2

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

+0

这取决于,特别是你想停止执行什么代码? –

+0

我正在构建一个简单的游戏,我使用委托来通知用户发生了某些事情。如果出现问题,我不会在项目中使用线程。我在主UIViewController中显示警报。 – kernix

回答

7

端起来:

myAlert.tag = UserConfirmationAlert; 

然后在你的UIAlertDelegate,可以将开关/箱中,像这样执行所需的方法

...

[alert show]; 

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

    } 
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和标签设置来完成此操作。

如果你有三个提醒,在您的类的顶部您@interface之前声明NSEnum像这样:

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

然后,右键在你的[警报显示],警报的标签设置为被显示。使用这种

// 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; 
    } 
}