1. APPDelegate中的代理方法可以监听到程序前后台切换的状态
- (void)applicationDidEnterBackground:(UIApplication * )application { NSLog(@"---applicationDidEnterBackground----") // 进入后台 } - (void)applicationDidBecomeActive:(UIApplication * )application { NSLog(@"---applicationDidBecomeActive----") // 进入前台 } - (void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"---applicationDidEnterBackground----") // 进入后台 } - (void)applicationDidBecomeActive:(UIApplication *)application { NSLog(@"---applicationDidBecomeActive----") // 进入前台 }
2. 单个ViewController通过在ViewDidLoad里注册筛选器通知来监听,在dealloc中移除通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someMethod1:) name:UIApplicationDidBecomeActiveNotification object:nil];
时间: 2024-10-10 23:50:43