检测何时从弹出窗口出现视图控制器 [英] Detect when view controller appears from pop

查看:19
本文介绍了检测何时从弹出窗口出现视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在导航堆栈的深处弹出一个视图控制器.是否可以检测视图控制器是通过推送还是弹出显示?

I am popping a view controller deep within a navigation stack. Is it possible to detect if the view controller is being shown from a push or a pop?

nav stack:

[A] -> [B] -> [C] -> [D] -> [E]

[E] 弹出到 [B]

[E] pops to [B]

nav stack:

[A]  -> [B] // Possible to detect if B appears from a pop?

推荐答案

在视图控制器 B 中,实现 viewWillAppearviewDidAppear.在那里,使用 isMovingToParentisBeingPresented 来查看它在什么条件下出现:

In view controller B, implement either viewWillAppear or viewDidAppear. In there, use isMovingToParent and isBeingPresented to see under what conditions it is appearing:

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

    if !isBeingPresented && !isMovingToParent {
        // this view controller is becoming visible because something that was covering it has been dismissed or popped
    }
}

以下是人们可能觉得方便的这些属性的更一般用法:

Below is a more general use of these properties that people may find handy:

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

    if isMovingToParent {
        // this view controller is becoming visible because it was just push onto a navigation controller or some other container view controller
    } else if isBeingPresented {
        // this view controller is becoming visible because it is being presented from another view controller
    } else if view.window == nil {
        // this view controller is becoming visible for the first time as the window's root view controller
    } else {
        // this view controller is becoming visible because something that was covering it has been dismissed or popped
    }
}

这篇关于检测何时从弹出窗口出现视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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