Sunday, June 9, 2013

UINavigationItem setBackBarButtonItem example in Objective C (iOS).


UINavigationItem setBackBarButtonItem

The bar button item to use when a back button is needed on the navigation bar.

@property(nonatomic, retain) UIBarButtonItem *backBarButtonItem

Discussion of [UINavigationItem setBackBarButtonItem]
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.

The default value of this property is nil.

UINavigationItem setBackBarButtonItem example.
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.

Example of [UINavigationItem setBackBarButtonItem].
This should be placed in the method that calls the ViewController titled "NewTitle". Right before the push or popViewController statement.

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"NewTitle" style: UIBarButtonItemStyleBordered target: nil action: nil];

[[self navigationItem] setBackBarButtonItem: newBackButton];

[newBackButton release];

UINavigationItem setBackBarButtonItem example.
UIImage *backButtonImage = [UIImage imageNamed:@"Graphics/Shared/navigation_back_button.png"];
CGRect frameimg = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
// you have to do your own backButtonAction to pop the view controller, b.t.w.
[someButton addTarget:self action:@selector(backButtonAction:)
     forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
[someButton release];

self.navigationItem.backBarButtonItem = backButton;

End of UINavigationItem setBackBarButtonItem example article.