一段时间后显示 UIAlertView
我试图在一段时间后显示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用 < code>NSTimer,为什么在这种情况下需要使用 GCD?
然后,在同一个类中,您将得到如下内容:
此外,正如 @PeyloW 指出的,您可以使用 < code>performSelector:withObject:afterDelay: 也是:
编辑 您现在还可以使用 GCD 的
dispatch_after
API:Why not use an
NSTimer
, why would you need to use GCD in this case?Then, within the same class, you'd have something like this:
Also, as @PeyloW noted, you can use
performSelector:withObject:afterDelay:
too:EDIT You can now also use GCD's
dispatch_after
API:这就是本地通知的创建目的。您可以设置类似 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.