2012-07-17 129 views
0

在我的應用程序要顯示的UIAlertView中的for循環UIAlertView中後暫停執行,並基於選擇「是」,「否」要顯示在for循環

我想執行下一步驟。由於UIAlertView不會暫停執行,我無法處理這種情況。反制全局和所有將使我的代碼更復雜。所以,我想暫停執行直到用戶選擇警報按鈕。

請讓我知道任何解決方案。

謝謝。

回答

0

首先添加<UIAlertViewDelegate>.h文件爲您的視圖控制器:

@interface ViewController : UIViewController <UIAlertViewDelegate> { 

然後創建警報,並顯示它當你需要它:

- (void)showConfirmAlert 
{ 
    UIAlertView *alert = [[UIAlertView alloc] init]; 
    [alert setTitle:@"Confirm"]; 
    [alert setMessage:@"Do you pick Yes or No?"]; 
    [alert setDelegate:self]; // Notice we declare the ViewController as the delegate 
    [alert addButtonWithTitle:@"Yes"]; 
    [alert addButtonWithTitle:@"No"]; 
    [alert show]; 
    [alert release]; 
} 

並實現委託方法捉按鈕點擊:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) 
    { 
     // Yes, do something 
    } 
    else if (buttonIndex == 1) 
    { 
     // No 
    } 
} 

Voila,yo你可以處理這兩種情況。

編輯:如果你有許多警報,宣佈他們都爲全局對象,所以你可以在alertView:clickedButtonAtIndex:方法每一個區分:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

    if (alertView == alertOne) { //alertOne is a globally declared UIAlertView 
     if (buttonIndex == 0) 
     { 
      // Yes, do something 
     } 
     else if (buttonIndex == 1) 
     { 
      // No 
     } 
    } else if (alertView == alertTwo) { //alertTwo is a globally declared UIAlertView 
     if (buttonIndex == 0) 
     { 
      // Yes, do something 
     } 
     else if (buttonIndex == 1) 
     { 
      // No 
     } 
    } 
} 
+0

利亞姆喬治Betsworth,感謝您的答覆,但我知道這種傳統的做法。但我想在循環中做到這一點,所以它不可能處理循環條件和所有。 – 2012-07-17 11:19:23

0

當顯示AlertView打破你的循環到時候再在用戶選擇上再次運行循環或進一步執行。順便說一下你想實現的目標是什麼?