viewWillDisappear: Determine whether view controller is being popped or is showing a sub-view controller
You can use the following.
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSArray *viewControllers = self.navigationController.viewControllers;
if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) {
// View is disappearing because a new view controller was pushed onto the stack
NSLog(@"New view controller was pushed");
} else if ([viewControllers indexOfObject:self] == NSNotFound) {
// View is disappearing because it was popped from the stack
NSLog(@"View controller was popped");
}
}
This is, of course, possible because the UINavigationController's view controller stack (exposed through the viewControllers property) has been updated by the time that viewWillDisappear is called.
Detect when view controller appears from pop
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
}
}
Checking if a UIViewController is about to get Popped from a navigation stack?
I don't think there is an explicit message for this, but you could subclass the UINavigationController and override - popViewControllerAnimated (although I haven't tried this before myself).
Alternatively, if there are no other references to the view controller, could you add to its - dealloc?
UIViewController viewDidAppear - because it was pushed or because subview was popped?
What you could do is have a BOOL property on your view controller, called alreadyPushed or something similar. Then in your viewDidAppear method check if it is false (Which it will be the first time the viewDidAppear is called), if it is set it to true.
Then when a subview viewController is popped the viewDidAppear method would check to see if the alreadyPushed property is set to true, if it is you know that a subview has just been popped.
How to be alerted when uiviewcontroller is pushed / popped from navigation stack
To find out when it's pushed, you can use the
UINavigationControllerDelegate
and implement
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
This method will fire whenever the viewcontroller is pushed into the navigation stack, and whenever the viewcontroller on top of it is popped off, thus revealing it again. So you have to use a flag to figure out if it's been initialized yet, if it hasn't means it just was pushed.
To find out when it's been popped, use this answer:
viewWillDisappear: Determine whether view controller is being popped or is showing a sub-view controller
Get notified when a view controller is about to be popped in iOS4
viewWillDisappear
is the appropriate delegate. You will need to add logic within this method if you want to determine if the current view is being popped or a new view is being pushed. That's been answered here - viewWillDisappear: Determine whether view controller is being popped or is showing a sub-view controller
Related Topics
How to Get the Day of the Week With Foundation
How to Detect the End of Loading of Uitableview
Do I Need to Disable Nslog Before Release Application
Add "...Read More" to the End of Uilabel
How to Get the Color of a Pixel in a Uiimage With Swift
How to Modify Swift_Module_Name
iOS Uitextview or Uilabel with Clickable Links to Actions
How to Change the Status Bar Background Color and Text Color on iOS 13
How to Receive Nsnotifications from Uiwebview Embedded Youtube Video Playback
Check For Internet Connection Availability in Swift
Closure Cannot Implicitly Capture a Mutating Self Parameter
Reason: No Suitable Image Found
Difference Between Viewdidload and Viewdidappear
How to Reload Data in a Tableview from a Different Viewcontroller in Swift