10

所以我想在我即将推出的 iPhone 应用程序中支持应用程序切换,并且我已经在我的应用程序委托中实现了所有正确的委托方法。因此,当用户恢复应用程序时,我可以在 NSLog 和所有内容中看到他们的活动。但是,我怎么知道我的应用程序已经恢复了控制器?有没有一种方法可以在我的控制器中告诉我应用程序已在所述控制器中恢复?我问的原因是因为我的应用程序处理自己的 URL 架构,并且我想根据启动的 URL 更新视图。任何帮助将不胜感激。

提前致谢

4

3 回答 3

20

您可以让您的控制器观察UIApplicationWillEnterForeground通知。它可能看起来像这样:

- (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
}
于 2010-08-21T01:53:18.490 回答
3

对于 Swift 4.2:

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



@objc func appWillEnterForeground() {
    // run when app enters foreground
}
于 2018-02-15T20:26:58.673 回答
-2

您也可以- (void)applicationDidBecomeActive:(UIApplication *)application在应用程序委托中进行覆盖,让它在它从后台返回时执行您希望它执行的任何操作。如果您希望特定视图而不是应用程序委托来获取消息,则需要按照上面 Elfred 的描述注册通知。

于 2010-08-23T15:33:45.943 回答