Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting action for back button in navigation controller

I'm trying to overwrite the default action of the back button in a navigation controller. I've provided a target an action on the custom button. The odd thing is when assigning it though the backbutton attribute it doesn't pay attention to them and it just pops the current view and goes back to the root:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc]                                    initWithTitle: @"Servers"                                    style:UIBarButtonItemStylePlain                                    target:self                                    action:@selector(home)]; self.navigationItem.backBarButtonItem = backButton; 

As soon as I set it through the leftBarButtonItem on the navigationItem it calls my action, however then the button looks like a plain round one instead of the arrowed back one:

self.navigationItem.leftBarButtonItem = backButton; 

How can I get it to call my custom action before going back to the root view? Is there a way to overwrite the default back action, or is there a method that is always called when leaving a view (viewDidUnload doesn't do that)?

like image 902
Parrots Avatar asked Jul 31 '09 21:07

Parrots


People also ask

Which type of controller will automatically create a back button?

When you move between view controllers using UINavigationController , it automatically configures a Back button show either “Back” or the title of the previous view controller.

How do I add a view controller to my navigation controller?

Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .

What is a navigation controller Swift?

A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface. In this type of interface, only one child view controller is visible at a time.


2 Answers

Try putting this into the view controller where you want to detect the press:

-(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 55
William Jockusch Avatar answered Sep 16 '22 13:09

William Jockusch


I've implemented UIViewController-BackButtonHandler extension. It does not need to subclass anything, just put it into your project and override navigationShouldPopOnBackButton method in UIViewController class:

-(BOOL) navigationShouldPopOnBackButton {     if(needsShowConfirmation) {         // Show confirmation alert         // ...         return NO; // Ignore 'Back' button this time     }     return YES; // Process 'Back' button click and Pop view controler } 

Download sample app.

like image 21
onegray Avatar answered Sep 17 '22 13:09

onegray