一段时间后显示UIAlertView [英] Displaying UIAlertView after some time

查看:110
本文介绍了一段时间后显示UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示一个UIAlertView一段时间后(如在应用程序中做某事后5分钟)。我已经通知用户,如果应用程序已关闭或在后台。但我想在应用程序运行时显示一个UIAlertView。

I'm trying to display a UIAlertView after some time (like 5 minutes after doing something in the app). I'm already notifying the user if the app is closed or in background. But I want to display a UIAlertView while the app is running.

我尝试了如下dispatch_async,但是警报永远弹出:

I tried to dispatch_async as follows but the alert is popping forever:

[NSThread sleepForTimeInterval:minutes];
 dispatch_async(dispatch_get_main_queue(),
       ^{
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"title!" message:@"message!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];
       }
       );

此外,我读到线程在30到60分钟后死机。

Also, I read that the thread dies after 30 to 60 minutes. I want to be able to display the alert after more than 60 min.

推荐答案

为什么不使用 NSTimer ,为什么在这种情况下需要使用GCD?

Why not use an NSTimer, why would you need to use GCD in this case?

[NSTimer scheduledTimerWithTimeInterval:5*60 target:self selector:@selector(showAlert:) userInfo:nil repeats:NO];

然后,在同一个类中,你会有这样的:

Then, within the same class, you'd have something like this:

- (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:

Also, as @PeyloW noted, you can use performSelector:withObject:afterDelay: too:

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];

EDIT 现在,您还可以使用GCD的 dispatch_after API:

EDIT You can now also use GCD's 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
});

这篇关于一段时间后显示UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆