How to tell when controller has resumed from background?

余生长醉 提交于 2019-11-28 09:17:55

You can have you your controller observe the UIApplicationWillEnterForeground notification. It probably would look something like this:

- (void) viewDidLoad
{
    [super viewDidLoad];
    //do stuff here
    if(&UIApplicationWillEnterForegroundNotification) { //needed to run on older devices, otherwise you'll get EXC_BAD_ACCESS
        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
        [notificationCenter addObserver:self selector:@selector(enteredForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
    }


}
- (void)enteredForeground:(NSNotification*) not
{
    //do stuff here
}
chawki

For Swift 4.2:

 NotificationCenter.default.addObserver(self,
 selector: #selector(appWillEnterForeground),
 name: UIApplication.willEnterForegroundNotification, 
 object: nil)



@objc func appWillEnterForeground() {
    // run when app enters foreground
}

You can also just override - (void)applicationDidBecomeActive:(UIApplication *)application in the app delegate to have it do whatever you want it to do when it comes back from the background. If you want a particular view to get the message rather than the app delegate you need to register for the notification as described by Elfred above.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!