I have a ViewControllers hierarchy, with UINavigationViewController as the root.
How can I find out whether some ViewController was started for the first time or it was started as a result of unwinding of the navigation stack?
Assuming you wish to know if viewWillAppear:
(or viewDidAppear:
) is being called when the view controller is first being displayed or if it's being displayed because another view controller has been dismissed, you can easily do the following:
Newer Swift versions:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if isBeingPresented || isMovingToParent {
// This is the first time this instance of the view controller will appear
} else {
// This controller is appearing because another was just dismissed
}
}
Older Swift versions:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if isBeingPresented() || isMovingToParentViewController() {
// This is the first time this instance of the view controller will appear
} else {
// This controller is appearing because another was just dismissed
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With