从后台打开应用程序时会调用什么 UIViewController 方法?

发布于 2024-09-17 01:12:38 字数 274 浏览 7 评论 0原文

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

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

记忆之渊 2024-09-24 01:12:38

Swift 5

订阅通知


       NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

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

删除通知


    override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)

            NotificationCenter.default.removeObserver(self)
    } 

Swift 5

Subscribe to Notification


       NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

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

Remove Notification


    override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)

            NotificationCenter.default.removeObserver(self)
    } 

萌吟 2024-09-24 01:12:38

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

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

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

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

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

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:

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.

扮仙女 2024-09-24 01:12:38

Combine 方法应该是:

private var cancellables: Set<AnyCancellable> = []

NotificationCenter.default
            .publisher(for: UIApplication.willEnterForegroundNotification, object: nil)
            .sink { [weak self] _ in
                print("app just returned to the foreground")
            }
            .store(in: &cancellables)

Combine approach should be:

private var cancellables: Set<AnyCancellable> = []

NotificationCenter.default
            .publisher(for: UIApplication.willEnterForegroundNotification, object: nil)
            .sink { [weak self] _ in
                print("app just returned to the foreground")
            }
            .store(in: &cancellables)
你在我安 2024-09-24 01:12:38
- (void)viewWillAppear:(BOOL)animated

不是

- (void)viewDidLoad

应用程序委托方法

- (void)applicationWillEnterForeground:(UIApplication *)applicationUIApplicationDelegate

将在应用程序进入前台后调用,尽管您可以为 < code>UIApplicationWillEnterForegroundNotification 在您的任何视图中。

- (void)viewWillAppear:(BOOL)animated

but not

- (void)viewDidLoad

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文