When a view loads, i want to see if it's because the user pressed the back button. How can i check this?
The solution is simple: create a Boolean property called goingForwards in your view controller, and set it to true before pushing any view controller onto the navigation stack, then set it back to false when the view controller is shown again.
The gesture recognizer responsible for popping the top view controller off the navigation stack.
in your viewWillDisappear method check
- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if ([self isMovingFromParentViewController]) { //specific stuff for being popped off stack } }
This is only for post iOS 5
The best solution I've found to detect a UINavigationController's back button press (pre-iOS 5.0) is by verifying that the current view controller is not present in the in the navigation controller's view controller stack.
It is possibly safer to check this condition in - (void)viewDidDisappear:(BOOL)animated
as logically, by the time that method is called it would be extremely likely that the view controller was removed from the stack.
Pre-iOS 5.0:
- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if (![[self.navigationController viewControllers] containsObject:self]) { // We were removed from the navigation controller's view controller stack // thus, we can infer that the back button was pressed } }
iOS 5.0+ you can use -didMoveToParentViewController:
- (void)didMoveToParentViewController:(UIViewController *)parent { // parent is nil if this view controller was removed }
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