当前位置 : 主页 > 手机开发 > ios >

ios – 从pop弹出时检测视图控制器

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在导航堆栈的深处弹出一个视图控制器.是否可以检测视图控制器是从推送还是弹出显示? nav stack:[A] - [B] - [C] - [D] - [E] [E]弹出[B] nav stack:[A] - [B] // Possible to detect if B appears from a po
我正在导航堆栈的深处弹出一个视图控制器.是否可以检测视图控制器是从推送还是弹出显示?

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来查看它出现在什么条件下:

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
    }
}
网友评论