2012-08-26 50 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; 
    } 
}