xuyannan's Blog

host on Github

监听APP从后台(background)切换到前台(foreground)

AppDelegate.m中的- (void)applicationWillEnterForeground:(UIApplication *)application;方法在APP即将转到foreground时会被调用。

具体的UIViewController中,可以在

-(void)viewWillAppear:(BOOL)animated;

方法里添加对这一事件的监听:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appWillEnterForegroundNotification)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:nil];

appWillEnterForegroundNotification即要调用的方法。

最后在UIViewController-(void)viewWillDisappear:(BOOL)animated中要移除这个observer

[[NSNotificationCenter defaultCenter] removeObserver:self];
comments powered by Disqus