Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

back button callback in navigationController in iOS

I have pushed a view onto the navigation controller and when I press the back button it goes to the previous view automatically. I want to do a few things when back button is pressed before popping the view off the stack. Which is the back button callback function?

like image 263
Namratha Avatar asked Mar 07 '11 09:03

Namratha


People also ask

How can remove back button in IOS?

To hide the back button on navigation bar we'll have to either set the navigation button as nil and then hide it or hide it directly. Let's create a project, add 2 view controller and Embed them in navigation controller.

How do I make a back button in Swift?

Back-button text is taken from parent view-controller's navigation item title. So whatever you set on previous view-controller's navigation item title, will be shown on current view controller's back button text. You can just put "" as navigation item title in parent view-controller's viewWillAppear method.


2 Answers

William Jockusch's answer solve this problem with easy trick.

-(void) viewWillDisappear:(BOOL)animated {     if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {        // back button was pressed.  We know this is true because self is no longer        // in the navigation stack.       }     [super viewWillDisappear:animated]; } 
like image 94
ymutlu Avatar answered Sep 19 '22 14:09

ymutlu


In my opinion the best solution.

- (void)didMoveToParentViewController:(UIViewController *)parent {     if (![parent isEqual:self.parentViewController]) {          NSLog(@"Back pressed");     } } 

But it only works with iOS5+

like image 38
Blank Avatar answered Sep 21 '22 14:09

Blank