检测视图控制器何时从弹出窗口出现
Detect when view controller appears from 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
查看它在什么条件下出现:
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
}
}
我正在导航堆栈深处弹出一个视图控制器。是否可以检测视图控制器是通过推送还是弹出显示?
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
}
}