我在导航堆栈深处弹出一个视图控制器.是否可以检测视图控制器是从推送还是弹出显示?
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 中,实现 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
}
}
这篇关于检测视图控制器何时从弹出窗口出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!