1

I have two UIViewcontrollers, let's call them vcA and vcB in an UINavigationController.

I want vcB to have a custom backbutton that triggers some code, the goal is do some custom animation

In vcA I put this code:

UIViewController *vcB = [UIViewController alloc] init]

UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                                                                      style:UIBarButtonItemStyleBordered 
                                                                     target:self 
                                                                     action:@selector(handleBack)];
self.navigationItem.backBarButtonItem = custombackBackButton;

[self.navigationController pushViewController: vcB animated: YES];

Then I added this code both in vcA and vcB:

-(void) handleBack
{
  NSlog(@"Going back to vcA");
}

The handleback method is never called. Any hint?

Thanks

Nicola

nico9T
  • 2,496
  • 2
  • 26
  • 44
  • 1
    http://stackoverflow.com/questions/1214965/setting-action-for-back-button-in-navigation-controller/3445994#3445994 – Baby Groot Mar 14 '13 at 09:00

4 Answers4

1

Use trick given by William Jockusch Setting action for Back Button

And also As per the Updating the Navigation Bar

  • If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.
  • If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItem property, the navigation bar displays that item.

So if you want to have custom selector You need to write this inside vcB NOT IN vbA

vcB.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                         style:UIBarButtonItemStyleBordered
                                                                        target:self
                                                                        action:@selector(handleBack)];
    self.navigationItem.backBarButtonItem = customBackButton;
}

-(void) handleBack
{
  NSlog(@"Going back to vcA");
}

NOTE:

UINavigationController Class Reference

When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. When this property is nil, the navigation item uses the value in its title property to create an appropriate back button. If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.

Here is is mentioned that If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) but it ignores custom view. So your selector is not invoking.

Community
  • 1
  • 1
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
1

Dont get me wrong but i think you need to be clear about push pop of UINavigationController.

A navigation controller manages views by pushing/popping them on/off the controller's view stack. When you push an item, the current view slides off screen to the left, and the new view slides over from the right. Ofcourse these animations can be changed according to your wish.

I think This is what you need completely.

Put this in vcA where you want to push vcB from vcA.

UIViewController *vcB = [UIViewController alloc] init];

[self.navigationController pushViewController: vcB animated: NO];

[UIView transitionWithView:self.navigationController.view
       duration:0.8
       options:UIViewAnimationOptionTransitionFlipFromRight
       animations:nil
       completion:nil];

In vcB, you can make a barbuttonitem

 UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:@"BackToVcA"
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;

In addActionMethod: you can put the below code for navigating back with your required animation

- (void)addAction:(id)sender
{
     UIViewController *vcA = [UIViewController alloc] init];

     [[self retain] autorelease];

     [self.navigationController pushViewController: vcA animated: NO];

     [UIView transitionWithView:self.navigationController.view duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];

     [self.navigationController popViewControllerAnimated:NO];

}
Sharanya K M
  • 1,805
  • 4
  • 23
  • 44
0

Your doing mistake. First Push to vcB and in vcB viewDidLoad method put this code.

UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(handleBack)]; 
 self.navigationItem.backBarButtonItem = custombackBackButton;

and also put this in vcB:

-(void) handleBack{
 NSlog(@"Going back to vcA");
}
Yashesh
  • 1,799
  • 1
  • 11
  • 29
  • This doesn't work. To change the title of the vcB backButton you have to do it from vcA, not from vcB viewDidLoad. – nico9T Mar 14 '13 at 09:01
  • I know it is not so user friendly for the programmer but to change the title of the vcB backButton you have to do it from vcA, not from vcB viewDidLoad. Yor trick works only if you set customBackButton to be a self.navigationItem.leftBarButtonItem, the rectangular one. I want the custom method to be called by a real back button, arrow shaped, and you can change it's title only in vcA, but the selector is never called. – nico9T Mar 14 '13 at 09:07
  • If you want to change the title of the back button then in your vcA put this . -(void)viewWillDisappear:(BOOL)animated{ self.navigationItem.title = @"Back"; } – Yashesh Mar 14 '13 at 09:08
  • and in vcB put -(void)viewWillAppear:(BOOL)animated{ self.navigationItem.title = @"vcB"; } – Yashesh Mar 14 '13 at 09:09
0

you can not modify the backBarButtonItems action , it is do the default (back) action , you should do your custom thing in the leftBarButtonItems

in your vcBs class add this :

- (void)viewWillAppear:(BOOL)animated
{
    UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                         style:UIBarButtonItemStyleBordered
                                                                        target:self
                                                                        action:@selector(handleBack)];
    self.navigationItem.leftBarButtonItem = customBackButton;
}

- (void)handleBack
{
    NSLog(@"back");
}
Guo Luchuan
  • 4,729
  • 25
  • 33