Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if user pressed the back button in uinavigationcontroller?

When a view loads, i want to see if it's because the user pressed the back button. How can i check this?

like image 914
Andrew Avatar asked May 23 '11 00:05

Andrew


People also ask

How to detect when the back button is tapped swift?

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.

What is Interactivepopgesturerecognizer?

The gesture recognizer responsible for popping the top view controller off the navigation stack.


2 Answers

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

like image 32
zach Avatar answered Sep 23 '22 20:09

zach


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 } 
like image 82
Andrew Avatar answered Sep 22 '22 20:09

Andrew