从后台打开应用程序时会调用什么 UIViewController 方法?
是否有任何方便的方法来确定是否正在从处于后台模式的应用程序加载视图?
在 3.XI 中,将依赖 viewDidLoad 来执行一些初始化等操作,但 4.X 中情况并非如此,因为您不能依赖调用 viewDidLoad 方法。
我想避免在 appdelegate 中添加额外的标志来检测这一点,我宁愿在 UIViewController 中使用可靠的方法来执行此操作,但似乎无法在 UIViewController 的生命周期中找到任何可以帮助我的东西。
有什么想法吗?你如何处理这种情况?
Is there any conventient way of determining if a view is being loaded from the app being in background mode?
In 3.X I would rely on viewDidLoad to do some initalization etc., this however is not the case for 4.X, as you cannot rely for the viewDidLoad method to be called.
I would like to avoid putting in extra flags to detect this in the appdelegate, I would rather use a reliable way of doing this in the UIViewController, but cannot seem to find anything in the lifecycle of a UIViewController that could help me out here.
Any ideas? How do you handle such situations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Swift 5
订阅通知
删除通知
Swift 5
Subscribe to Notification
Remove Notification
UIViewController 的生命周期没有将应用程序从后台移动到前台时调用的方法。
当您希望此事件触发某些特定的代码块时,您需要为名为
Notification.Name.UIApplicationWillEnterForeground
的通知添加观察者。一个例子是:请记住,您将需要删除观察者以防止它在整个应用程序中触发。
UIViewController's lifecycle has no methods that will be called when moving an app from background to foreground.
When you want this event to trigger some specific block of code you need to add an observer for notification named
Notification.Name.UIApplicationWillEnterForeground
. An example of this would be:Keep in mind that you will need to remove the observer to prevent it from triggering throughout the application.
Combine
方法应该是:Combine
approach should be:但不是
应用程序委托方法
- (void)applicationWillEnterForeground:(UIApplication *)applicationUIApplicationDelegate
将在应用程序进入前台后调用,尽管您可以为 < code>UIApplicationWillEnterForegroundNotification 在您的任何视图中。
but not
The Application Delegate Method
- (void)applicationWillEnterForeground:(UIApplication *)applicationUIApplicationDelegate
will be called after the the application entered the foreground though you can add an observer for the
UIApplicationWillEnterForegroundNotification
in any of your views.