iOS---在controller里面自定义方法判断:home键进入后台,返回前台

iOS—在controller里面自定义方法判断:home键进入后台,返回前台

1、定义进入前台时调用的函数:
- (void)applicationWillEnterForeground:(NSNotification *)notification
{
//进入前台时调用此函数
我们可以在这个方法里写上我们需要刷新的代码
}
2、注册调用上面函数的通知,在willAppear中:
- (void)viewWillAppear:(BOOL)animated
{
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:app];
}

3、反注册通知,在willDisappear中:
- (void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

你可能感兴趣的:(iOS)