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

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

问题描述

我正在将视图控制器弹出到导航堆栈的深处。

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]

nav stack:

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


推荐答案

在视图控制器B中,实现 viewWillAppear viewDidAppear 。在其中,使用 isMovingToParent isBeingPresented 来查看它在什么条件下出现:

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天全站免登陆