为什么不使用NSTimer
,为什么在这种情况下需要使用 GCD ?
[NSTimer scheduledTimerWithTimeInterval:5*60 target:self selector:@selector(showAlert:) userInfo:nil repeats:NO];
然后,在同一个班级中,你会有这样的事情:
- (void) showAlert:(NSTimer *) timer {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"title!"
message:@"message!"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
}
此外,正如@PeyloW 所指出的,您也可以使用performSelector:withObject:afterDelay:
:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"title!"
message:@"message!"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert performSelector:@selector(show) withObject:nil afterDelay:5*60];
[alert release];
编辑您现在还可以使用 GCD 的dispatch_after
API:
double delayInSeconds = 5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title!"
message:@"message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alertView show];
[alertView release]; //Obviously you should not call this if you're using ARC
});