如何确定第一次启动UIViewController? [英] How to determine that UIViewController was started for the first time?

查看:108
本文介绍了如何确定第一次启动UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewControllers层次结构,UINavigationViewController作为根。

如何找出一些ViewController是第一次启动还是由于导航堆栈的展开而启动?

I have a ViewControllers hierarchy, with UINavigationViewController as the root.
How can I find out whether some ViewController was started for the first time or it was started as a result of unwinding of the navigation stack?

推荐答案

假设你想知道 viewWillAppear:(或 viewDidAppear:)在首次显示视图控制器时被调用,或者由于另一个视图控制器被解除而被显示,您可以轻松执行以下操作:

Assuming you wish to know if viewWillAppear: (or viewDidAppear:) is being called when the view controller is first being displayed or if it's being displayed because another view controller has been dismissed, you can easily do the following:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    if isBeingPresented() || isMovingToParentViewController() {
        // This is the first time this instance of the view controller will appear
    } else {
        // This controller is appearing because another was just dismissed
    }
}

这篇关于如何确定第一次启动UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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