2009-05-19 49 views
4

我使用多個UIAlertViews在我的代碼如下UIAlertView中不等待

-(void) myfunc 
{ 
    myAlertView1 = [[UIAlertView alloc] initWithTitle:@"Message" message:[list objectAtIndex:1] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
    [myAlertView1 show]; 
    [myAlertView1 release], myAlertView1 = nil; 
    { 
     do something 
    } 
    myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[list   objectAtIndex:1] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
    [myAlertView show]; 
    [myAlertView release], myAlertView = nil; 
} 

當我運行在模擬器 我看到myAlertView1(消息),簡單的程序,它不等待確定按鈕單擊 然後我看到myAlertView(錯誤),等待Ok按鈕單擊,然後再次看到myAlertView1(消息),並等待,直到單擊OK按鈕。

從邏輯上看,我想看到myAlertView1(消息)並等待,直到點擊確定按鈕,然後看到myAlertView(錯誤)並等到按鈕被點擊。 我在這裏錯過了什麼嗎?

回答

12

UIAlertView不是人們所期望的那樣。你應該等待你的代表在創建和顯示第二個之前接收alertView:didDismissWithButtonIndex:UIAlertView

0

這裏是你如何使你的對話模態。

我在研究MonoTouch/C#用戶的類似問題時遇到了這個問題,所以我爲他寫了這個示例。同樣的例子可以簡單地移植到Objective-C。

要做到這一點,你可以做的是手動運行主循環。我沒有設法直接停止主循環,所以我運行0.5秒的主循環並等待用戶響應。

下面的函數展示瞭如何實現上述方法模態查詢:

int WaitForClick() 
{ 
    int clicked = -1; 
    var x = new UIAlertView ("Title", "Message", null, "Cancel", "OK", "Perhaps"); 
    x.Show(); 
    bool done = false; 
    x.Clicked += (sender, buttonArgs) => { 
     Console.WriteLine ("User clicked on {0}", buttonArgs.ButtonIndex); 
    clicked = buttonArgs.ButtonIndex; 
    };  
    while (clicked == -1){ 
     NSRunLoop.Current.RunUntil (NSDate.FromTimeIntervalSinceNow (0.5)); 
     Console.WriteLine ("Waiting for another 0.5 seconds"); 
    } 

    Console.WriteLine ("The user clicked {0}", clicked); 
    return clicked; 
} 
+0

沒有在iOS上運行。 在進入while循環之前,UIAlertView本身沒有顯示。 – Gagan 2013-10-04 14:43:08

0

在這裏,您可以如下做到這一點:

-(void) myfunc 
{ 
    myAlertView1 = [[UIAlertView alloc] initWithTitle:@"Message" message:[listobjectAtIndex:1] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
    [myAlertView1 show]; 
    [myAlertView1 release], myAlertView1 = nil; 
    { 
     do something 
    } 
} 

當你得到alertview,你可以點擊OK按鈕,調用另一個方法打開另一個alertview。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if(buttonIndex==0) 
    { 
     [self open_secondAlert]; 
    } 
} 

-(void)open_secondAlert 
{ 
    myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[list objectAtIndex:1] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
    [myAlertView show]; 
    [myAlertView release], myAlertView = nil; 
} 

如果您還有任何疑問,請讓我知道。

感謝, 最好的問候, Gurprit

+0

ARC禁止「發佈」的顯式消息發送。 – parsley72 2014-11-07 00:52:53