Rotten
Rotten

Reputation: 742

UINavigationItem BackBarButtonItem not replaced

I'm having a curious issue with backBarButtonItem. I want to replace its title in the entire application for "Back" and replacing the back button works in most of -viewDidLoad event but in other views it's not working and show the name of the previous view. Has someone has the same problem?

P.S. The way to replace the backBarButtonItem is the standard one instantiating an UIBarButtonItem and setting it to viewController.navigationIten.backBarButtonItem property.

Upvotes: 3

Views: 3808

Answers (3)

Rotten
Rotten

Reputation: 742

Well, at last I've found the solution to this issue.

If you want that any backBarButtonItem of your application has the same title a good approach is to subclass UINavigationController and override - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated to replace the back button.

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BackButtonLabel", "")
                                                                    style:UIBarButtonItemStyleDone 
                                                                   target:nil 
                                                                   action:nil];
    viewController.navigationItem.backBarButtonItem = _backButton;
    _backButton = nil;
    [_backButton release];

    [super pushViewController:viewController animated:animated];
}

By this way every back button in your application will have the same title.

I hope this will be helpful for anyone else.

Upvotes: 1

Paras Joshi
Paras Joshi

Reputation: 20551

when you push the view from your current view at that time after allocate your next viewcontroller object ,just put bellow line

YourViewController *objView = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
          self.navigationItem.title=@"Back";
            [self.navigationController pushViewController:objView animated:YES];

your Next View will Appear with Back Button.... :)

Upvotes: 1

omz
omz

Reputation: 53561

The backBarButtonItem does not set the back button that is shown in the current view, it sets the back button that navigates to the current view, i.e. the back button in the next view.

This makes sense because the back button's title is usually the title of the previous view controller.

If you want to set the left button in the navigation bar directly, use self.navigationItem.leftBarButtonItem.

Upvotes: 16

Related Questions