当从后台打开应用程序时,不会调用ViewDidAppear

我有一个视图控制器,其中我的价值是0(标签),当我从另一个ViewController打开该视图控制器我已经设置viewDidAppear设置值20标签。 它工作正常,但是当我closures我的应用程序,并再次打开我的应用程序,但值不会改变,因为viewDidLoadviewDidAppearviewWillAppear没有被调用。 打开我的应用程序时我怎么打电话 我必须从applicationDidBecomeActive做些什么吗?

对事件的确切顺序感兴趣,我安装了一个应用程序,如下所示:(@Zohaib,你可以使用下面的NSNotificationCenter代码来回答你的问题)。

 // AppDelegate.m - (void)applicationWillEnterForeground:(UIApplication *)application { NSLog(@"app will enter foreground"); } - (void)applicationDidBecomeActive:(UIApplication *)application { NSLog(@"app did become active"); } // ViewController.m - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"view did load"); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; } - (void)appDidBecomeActive:(NSNotification *)notification { NSLog(@"did become active notification"); } - (void)appWillEnterForeground:(NSNotification *)notification { NSLog(@"will enter foreground notification"); } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSLog(@"view will appear"); } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"view did appear"); } 

在启动时,输出如下所示:

 2013-04-07 09:31:06.505 myapp[15459:11303] view did load 2013-04-07 09:31:06.507 myapp[15459:11303] view will appear 2013-04-07 09:31:06.511 myapp[15459:11303] app did become active 2013-04-07 09:31:06.512 myapp[15459:11303] did become active notification 2013-04-07 09:31:06.517 myapp[15459:11303] view did appear 

input背景,然后重新input前景:

 2013-04-07 09:32:05.923 myapp[15459:11303] app will enter foreground 2013-04-07 09:32:05.924 myapp[15459:11303] will enter foreground notification 2013-04-07 09:32:05.925 myapp[15459:11303] app did become active 2013-04-07 09:32:05.926 myapp[15459:11303] did become active notification 

使用Objective-C

您应该在您的ViewControllerviewDidLoad方法中注册一个UIApplicationWillEnterForegroundNotification ,并且每当应用程序从后台返回时,您都可以在注册通知的方法中执行任何您想要执行的操作。 ViewControllerviewWillAppearviewDidAppear不会被调用,当应用程序从背景回到前台。

 -(void)viewDidLoad{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doYourStuff) name:UIApplicationWillEnterForegroundNotification object:nil]; } -(void)doYourStuff{ // do whatever you want to do when app comes back from background. } 

不要忘记取消注册您注册的通知。

 -(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } 

请注意,如果您注册了viewController for UIApplicationDidBecomeActiveNotification则每当您的应用程序变为活动状态时都会调用您的方法,因此不build议为此通知注册viewController

使用Swift

添加观察者可以使用下面的代码

  override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector:"doYourStuff", name: UIApplicationWillEnterForegroundNotification, object: nil) } func doYourStuff(){ // your code } 

要删除观察者,可以使用swift的deinit函数。

 deinit { NSNotificationCenter.defaultCenter().removeObserver(self) } 

Swift 3.0版本

在你的viewDidLoad ,在通知中心注册,听从后台操作打开

 NotificationCenter.default.addObserver(self, selector:#selector(doSomething), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil) 

然后添加此function并执行所需的操作

 func doSomething(){ //... } 

最后添加这个函数来清理视图控制器被销毁时的通知观察者。

 deinit { NotificationCenter.default.removeObserver(self) } 

我认为注册UIApplicationWillEnterForegroundNotification是有风险的,因为您最终可能会有多个控制器对该通知作出反应。 没有任何担心的是,收到通知时,这些控制器仍然可见。

这是我做的:我直接从应用程序的委托didBecomeActive方法强制在主动控制器上调用viewDidAppear:

将以下代码添加到- (void)applicationDidBecomeActive:(UIApplication *)application

 UIViewController *activeController = window.rootViewController; if ([activeController isKindOfClass:[UINavigationController class]]) { activeController = [(UINavigationController*)window.rootViewController topViewController]; } [activeController viewDidAppear:NO]; 

只要让您的视图控制器注册UIApplicationWillEnterForegroundNotification通知,并作出相应的反应。

尝试在AppDelegate applicationWillEnterForeground中添加这个。

 func applicationWillEnterForeground(_ application: UIApplication) { // makes viewWillAppear run self.window?.rootViewController?.beginAppearanceTransition(true, animated: false) self.window?.rootViewController?.endAppearanceTransition() } 

根据苹果的文件:

 (void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated; 

描述:
告诉小孩控制器其外观即将改变。 如果您正在实现自定义容器控制器,请使用此方法告诉孩子其视图即将出现或消失不要直接调用viewWillAppear:viewWillDisappear:viewDidAppear:viewDidDisappear:

 (void)endAppearanceTransition; 

描述:

告诉小孩控制器它的外观已经改变了。 如果您正在实现自定义容器控制器,请使用此方法告诉子视图转换已完成。

示例代码:

 (void)applicationDidEnterBackground:(UIApplication *)application { [self.window.rootViewController beginAppearanceTransition: NO animated: NO]; // I commented this line [self.window.rootViewController endAppearanceTransition]; // I commented this line } 

问题: 我如何解决?

Ans :我在应用程序中find了这条线。 这行使我的应用程序不接收任何ViewWillAppear通知。 当我评论这些线路的工作正常