检测应用程序何时进入我的视图的背景时,最好的方法是什么? [英] What's the best way to detect when the app is entering the background for my view?

查看:96
本文介绍了检测应用程序何时进入我的视图的背景时,最好的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器,它使用 NSTimer 来执行一些代码。

I have a view controller that uses an NSTimer to execute some code.

最好的方法是什么检测应用程序何时进入后台,以便暂停计时器?

What's the best way to detect when the app is going to the background so I can pause the timer?

推荐答案

您可以让任何课程感兴趣应用程序进入后台接收通知。这是将这些类与AppDelegate耦合的好方法。

You can have any class interested in when the app goes into the background receive notifications. This is a good alternative to coupling these classes with the AppDelegate.

初始化所述类时:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];

回应通知

-(void)appWillResignActive:(NSNotification*)note
{

}
-(void)appWillTerminate:(NSNotification*)note
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];

}

这篇关于检测应用程序何时进入我的视图的背景时,最好的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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