检测何时从弹出窗口显示视图控制器

时间:2019-01-15 21:32:50

标签: ios cocoa-touch uiviewcontroller uinavigationcontroller

我在导航堆栈的深处弹出一个视图控制器。是否可以通过推送或弹出窗口检测视图控制器的显示状态?

nav stack:

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

[E]弹出到[B]

nav stack:

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

1 个答案:

答案 0 :(得分:8)

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

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
    }
}

以下是人们可能会用到的这些属性的更一般用法:

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
    }
}