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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (28)

我有一个视图控制器,其中我的值是0(标签),当我从另一个视图控制器打开时,ViewController我已设置viewDidAppear为在标签上设置值20。它工作正常,但是当我关闭我的应用程序,比再次我打开我的应用程序,但价值不因为改变viewDidLoadviewDidAppearviewWillAppear没有被调用。打开我的应用程序时,我该如何打电话。我需要做什么applicationDidBecomeActive

提问于
用户回答回答于

对事件的确切顺序感到好奇,我安装了一个应用程序,如下所示:

// 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

输入背景,然后重新输入前景:

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

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

-(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];
}

请注意,如果你注册viewControllerUIApplicationDidBecomeActiveNotification你的方法,那么每次你的应用程序变为活动状态时都会调用您的方法,因此不建议注册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)
}

扫码关注云+社区