为什么当应用程序从后台返回时,不会调用viewWillAppear? [英] Why does viewWillAppear not get called when an app comes back from the background?

查看:125
本文介绍了为什么当应用程序从后台返回时,不会调用viewWillAppear?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写应用程序,如果用户在通话时查看应用程序,我需要更改视图。

I'm writing an app and I need to change the view if the user is looking at the app while talking on the phone.

我已经实现了以下方法:

I've implemented the following method:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear:");
    _sv.frame = CGRectMake(0.0, 0.0, 320.0, self.view.bounds.size.height);
}

但是当应用程序返回前台时,它没有被调用。

But it's not being called when the app returns to the foreground.

我知道我可以实现:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

但我不想这样做。我宁愿将所有布局信息放在viewWillAppear:方法中,让它处理所有可能的场景。

but I don't want to do this. I'd much rather put all my layout information in the viewWillAppear: method, and let that handle all possible scenarios.

我甚至试图从applicationWillEnterForeground调用viewWillAppear: :,但我似乎无法确定当前哪个是当前的视图控制器。

I've even tried to call viewWillAppear: from applicationWillEnterForeground:, but I can't seem to pinpoint which is the current view controller at that point.

有人知道处理这个的正确方法吗?我确定我错过了一个明显的解决方案。

Does anybody know the proper way to deal with this? I'm sure I'm missing an obvious solution.

推荐答案

方法 viewWillAppear 应该在您自己的应用程序中发生的事情的上下文中进行,而不是在您从另一个应用程序切换回应用程序时将应用程序置于前台的上下文中。

The method viewWillAppear should be taken in the context of what is going on in your own application, and not in the context of your application being placed in the foreground when you switch back to it from another app.

换句话说,如果有人查看另一个应用程序或接听电话,然后切换回您之前在后台运行的应用程序,您离开应用程序时已经可见的UIViewController不会可以这么说 - 就它而言,它永远不会消失而且仍然可见 - 因此 viewWillAppear 不会被调用。

In other words, if someone looks at another application or takes a phone call, then switches back to your app which was earlier on backgrounded, your UIViewController which was already visible when you left your app 'doesn't care' so to speak -- as far as it is concerned, it's never disappeared and it's still visible -- and so viewWillAppear isn't called.

我建议不要自己调用 viewWillAppear - 它有一个特定的含义,你不应该颠覆它!你可以做的重构可以达到同样的效果:

I recommend against calling the viewWillAppear yourself -- it has a specific meaning which you shouldn't subvert! A refactoring you can do to achieve the same effect might be as follows:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self doMyLayoutStuff:self];
}

- (void)doMyLayoutStuff:(id)sender {
    // stuff
}

然后你也从适当的通知中触发 doMyLayoutStuff

Then also you trigger doMyLayoutStuff from the appropriate notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doMyLayoutStuff:) name:UIApplicationDidChangeStatusBarFrameNotification object:self];

顺便说一下,没有开箱即用的方法来判断哪个是'当前'的UIViewController。但你可以找到解决方法,例如有UINavigationController的委托方法,用于找出何时在其中呈现UIViewController。您可以使用这样的东西来跟踪已经呈现的最新UIViewController。

There's no out of the box way to tell which is the 'current' UIViewController by the way. But you can find ways around that, e.g. there are delegate methods of UINavigationController for finding out when a UIViewController is presented therein. You could use such a thing to track the latest UIViewController which has been presented.

更新

如果您在各个位上使用适当的自动调整掩码布局UI,有时您甚至不需要处理UI中的手册 - 它只是处理...

If you layout out UIs with the appropriate autoresizing masks on the various bits, sometimes you don't even need to deal with the 'manual' laying out of your UI - it just gets dealt with...

这篇关于为什么当应用程序从后台返回时,不会调用viewWillAppear?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆