2012-07-14 15 views
1

林顯示兩個警報視圖陸續陸續有以下之前:如何添加延遲呈現第二個提醒查看

-IBAction

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"my message"  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[alert setDelegate:self]; 

[alert show]; 




} 

- (void)didPresentAlertView:(UIAlertView *)alertView 
{ 

[alertView setTitle:@"My new title"]; 
[alertView setMessage:@"My new message"]; 

} 

從第一警報視圖到第二的過渡是用戶很快就沒有時間閱讀第一條消息。有人可能會建議如何在警報之間添加延遲。我認爲我需要實現一個NSTimer,但是實現這個是我可以使用一些建議的地方。

+0

時使用的警報視圖**駁回被稱爲UIAlertViewDelegate協議方法* * – 2012-07-14 13:54:13

回答

13

我會建議使用dispatch_after,可內聯:

double delayInSeconds = 2.0; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    // code to be executed on the main queue after delay 
}); 
+0

謝謝你的工作,這是保存到我的代碼片段! – JSA986 2012-07-14 14:10:33

+0

真棒好友:) – 2014-04-07 05:05:25

0

試試這個簡單的方法:

- (void)alertView 
{ 
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Get Ready!" message:nil  delegate:nil cancelButtonTitle:nil otherButtonTitles:nil]; 
    [alertView show]; 
    [self performSelector:@selector(dismissStartAlert:) withObject:alertView afterDelay:5]; 
} 

-(void)dismissStartAlert:(UIAlertView *)alertView 
{ 
    [alertView dismissWithClickedButtonIndex:0 animated:YES]; 
    [alertView setTitle:@"My new title"]; 
    [alertView setMessage:@"My new message"]; 
    [alert show]; 
}