18

I have a UITableViewController that launches a UIViewController and I would like to trap whenever the back button is pressed in the child controller, which is the class that derives from 'UIViewController'. I can change the Back Button title but setting the target & action values when setting the backBarButtonItem seems to get ignored. What's a way to receiving some kind of notification that the Back button was tapped?

- (void)showDetailView 
{
    // How I'm creating & showing the detail controller
    MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyDetailView" bundle:nil];   

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Pages"
                            style:UIBarButtonItemStyleBordered 
                            target:self                                     
                            action:@selector(handleBack:)];

    self.navigationItem.backBarButtonItem = backButton;
    [backButton release];

    [self.navigationController pushViewController:controller animated:animated];
    [controller release];

}   

- (void)handleBack:(id)sender
{
    // not reaching here
    NSLog(@"handleBack event reached");
}
1

8 Answers 8

19

You can implement the viewWillDisappear method of UIViewController. This gets called when your controller is about to go away (either because another one was pushed onto the navigation controller stack, or because the 'back' button was pressed).

To determine whether the view is disappearing because of the back button being pressed, you can use a custom flag that you set wherever you push a new controller onto the navigation controller, like shown below

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (viewPushed) {
        viewPushed = NO;   // Flag indicates that view disappeared because we pushed another controller onto the navigation controller, we acknowledge it here
    } else {
        // Here, you know that back button was pressed
    }   
}

And wherever you push a new view controller, you would have to remember to also set that flag...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    viewPushed = YES;
    [self.navigationController pushViewController:myNewController animated:YES];
    ...
}
6
  • How can you know that the view didn't disappear because some other button invoked a child view? I want to know only when the "Back" button is pressed. Oct 15, 2009 at 16:57
  • In cases like this, I set a flag in didSelectRowAtIndexPath: (where you usually push a new controller onto the navigation controller). viewWillDisappear can then inspect that flag and deduce whether the back button was pressed or not Nov 6, 2009 at 10:43
  • That still doesn't address the problem Justin pointed out; With your flag you will know how the view came into existence, but that doesn't mean that's the only way it will disappear.
    – kbanman
    Jan 25, 2010 at 7:08
  • It does, because you're in control of what gets pushed onto the navigation controller. If you do it right (ie, you don't forget any cases), the only case left where your flag isn't set and your view is disappearing is when the user pressed the back button. This works great for me. Jan 30, 2010 at 7:43
  • depreceated this answer
    – Esqarrouth
    Feb 16, 2014 at 13:34
17

It has been a while since this was asked, but I just tried to do this myself. I used a solution similar to Zoran's, however instead of using a flag I did this:

- (void)viewWillDisappear: (BOOL)animated
{
    [super viewWillDisappear: animated];
    if (![[self.navigationController viewControllers] containsObject: self])
    {
        // the view has been removed from the navigation stack, back is probably the cause
        // this will be slow with a large stack however.
    }
}

I think it bypasses the issues with flags and IMO is cleaner, however not as efficient (if there are lots of items on the navigation controller).

4
  • 6
    You might be able to use [self.navigationController.viewControllers indexOfObjectIdenticalTo: self] != NSNotFound instead of containsObject, which would only compare the pointer address. Jun 12, 2012 at 20:51
  • 1
    If your controller is contained within another container, you'll have to compare via: "indexOfObjectIdenticalTo:self.parentViewController", other than that, works.
    – marmor
    Apr 17, 2013 at 8:15
  • if (([self.parentViewController.navigationController.viewControllers indexOfObjectIdenticalTo: self] != NSNotFound)) worked for me.
    – SmileBot
    Jul 29, 2013 at 20:37
  • 1
    It is plenty efficient. This isn't code that needs to be optimized.
    – Dustin
    Jan 2, 2015 at 20:57
14

In my opinion the best solution.

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

But it only works with iOS5+

1
  • This is the best solution I think, since it's more self-contained.
    – canhazbits
    Jan 31, 2014 at 4:20
9

I use this code:

- (void) viewWillDisappear:(BOOL)animated {

   if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound)
   {
      // your view controller already out of the stack, it meens user pressed Back button
   }
}

But this is not actual when user presses tab bar button and pops to root view controller at one step. For this situation use this:

   [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(viewControllerChange:)
                                                name:@"UINavigationControllerWillShowViewControllerNotification"
                                              object:self.navigationController];


- (void) viewControllerChange:(NSNotification*)notification {

   NSDictionary* userInfo = [notification userInfo];

   if ([[userInfo objectForKey:@"UINavigationControllerNextVisibleViewController"] isKindOfClass:[<YourRootControllerClass> class]])
   { 
      // do your staff here
   }
}

Don't forget then:

   [[NSNotificationCenter defaultCenter] removeObserver:self
                                                   name:@"UINavigationControllerWillShowViewControllerNotification"
                                                 object:self.navigationController];
1

You can make your own button and place it as the leftBarButtonItem. Then have it call your method where you can do whatever, and call [self.navigationController popViewController... yourself

2
  • 1
    the 'leftBarButtonItem' won't show the same button style with the sideways looking triangle. Instead I think the button will be square. Is there a way to make it look like the typical backbutton as well as trap the event? Oct 12, 2009 at 23:07
  • You can create your own left arrow image if you know the width of your button text -- or you could create it programmatically, mimicking what Apple does.
    – coneybeare
    Oct 12, 2009 at 23:32
1
{
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"back"
                                                                   style:UIBarButtonItemStyleBordered 
                                                                  target:self                                                                             
                                                                  action:@selector(handleBack:)];
    self.navigationItem.leftBarButtonItem = backButton;
    [backButton release];
    [self filldata];
    [super viewDidLoad];
}

just replace backBarButtonItem with leftBarButtonItem

1
  • This seems like the best answer to me. Why doesn't this answer have more upvotes? Am I overlooking something?
    – daniel
    Sep 29, 2022 at 3:55
0

This seems possible now as of iOS16+ with the help of backAction

Here is what the documentation says:

If a back button already appears in the navigation bar, setting this property replaces its action without modifying its appearance.

So in your viewDidLoad or somewhere appropriate, you can add this code:

navigationItem.backAction = UIAction { [weak self] _ in
    // Handle your other logic here for back button tap
    self?.navigationController?.popViewController(animated: true)
    
}
0
-2

Simply use viewDidDisappear instead. It will be perfectly called in any scenario.

We are basing your lifecycle management on viewDidAppear and viewDidDisappear. If you know Android: the both are comparable to onResume and onPause methods. But there is a difference when it comes to locking the screen or pressing the homebutton on iOS.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.