一段时间后显示 UIAlertView

发布于 2024-10-10 11:43:33 字数 539 浏览 0 评论 0原文

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

我尝试如下进行dispatch_async,但警报永远弹出:

[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分钟后死亡。我希望能够在超过 60 分钟后显示警报。

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.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你是孤单的心事 2024-10-17 11:43:33

为什么不使用 < code>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 指出的,您可以使用 < code>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
});

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

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 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
});
若无相欠,怎会相见 2024-10-17 11:43:33

这就是本地通知的创建目的。您可以设置类似 UIAlertView 的通知在将来某个时间出现,即使您的应用程序处于后台或根本没有运行。

这里是一个教程。

This is the kind of thing that Local Notifications were created for. You can set an UIAlertView-like notification to come up some time in the future, even if your app is backgrounded or not running at all.

Here is a tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文