从后台打开应用程序时调用了什么UIViewController方法? [英] What UIViewController method is called when opening app from background?

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

问题描述

是否有任何方便的方法来确定是否正在从处于后台模式的应用程序加载视图?

Is there any conventient way of determining if a view is being loaded from the app being in background mode?

在3.XI中依赖于viewDidLoad来做某些事情但是,这不是4.X的情况,因为你不能依赖于调用viewDidLoad方法。

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.

我想避免额外投入在appdelegate中检测这个的标志,我宁愿在UIViewController中使用一种可靠的方法,但是在UIViewController的生命周期中似乎无法找到任何可以帮助我的东西。

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?

推荐答案

UIViewController的生命周期没有在将应用程序从后台移动到前台时调用的方法。

UIViewController's lifecycle has no methods that will be called when moving an app from background to foreground.

当您希望此事件触发某些特定代码块时,您需要为名为 Notification.Name.UIApplicationWillEnterForeground 的通知添加观察者。一个例子是:

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:

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

@objc func appMovedToForeground() {
    //Your code here
}

请记住,您需要移除观察者以防止它在整个应用程序中触发。

Keep in mind that you will need to remove the observer to prevent it from triggering throughout the application.

这篇关于从后台打开应用程序时调用了什么UIViewController方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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