Adding a UIBarButtonItem programmatically to UINavigationBar
Solution 1
You are trying to set the Back Button Item in a modal view which doesn't add a backBarButtonItem. This what causes the Button (or any sort of back button for that matter) not to show. The backBarButtonItem is mainly for use with Pushed View Controllers which have a Back Button added from the parent (next item below) when you push a new view controller (top item). The Apple UINavigationItem Documentation says:
When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.
To get the Back Button on the left side like you wish, Try changing
self.navigationItem.backBarButtonItem = backButton;
to
self.navigationItem.leftBarButtonItem = backButton;
Solution 2
making a call such as this from a view controller
{
NextViewController* vcRootView = [[NextViewController alloc] initWithNibName:@"NextView" bundle:[NSBundle mainBundle]];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vcRootView];
[vcRootView release];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
}
will present NextViewController as a Modal view on the calling view and NextViewController will have a navigationController for it.
In The NextViewController implementation file all you need is this
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self
action:@selector(barButtonBackPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
}
-(void)barButtonBackPressed:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
to have the back button to dismiss the modalview. Hope it helps.
Solution 3
Use below code snippet :
//Add button to NavigationController
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@“back”, @"")
style:UIBarButtonItemStylePlain
target:self
action:@selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
//Perform action on back Button
- (void) goBack { // Go back task over-here
}
Different style types available are :
UIBarButtonItemStylePlain, UIBarButtonItemStyleBordered, UIBarButtonItemStyleDone
Solution 4
You may use this setters without creation new UIBarButtonItem:
[self.navigationItem.leftBarButtonItem setAction:@selector(doBackButton:)];
[self.navigationItem.leftBarButtonItem setTarget:self];
Crystal
Updated on July 05, 2022Comments
-
Crystal almost 2 years
I dropped in a
UINavigationBar
inUIInterfaceBuilder
. I present this view modally and just want aUIBackBarButton
to return to my last view. I have an outlet and property to thisUINavigationBar
declared. I thought in myviewDidLoad
method, I could create aUIBackButton
like this:UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered target:self action:@selector(goBack)]; self.navigationItem.backBarButtonItem = backButton; [backButton release];
But I do not see my
UIBackBarButtonItem
on theUINavigationBar
. I think I am doing something wrong here since I don't think myUINavigationBar
knows I'm trying to add thisUIBackBarButtonItem
to it in this way. Would I have to do create anNSArray
, put the button in it, and setItems for the NavigationBar instead?I'm confused on how the navigationItem property works vs the setItems of the
UINavigationBar
as well. Any help would be appreciated. Thanks! -
Gabriel Tomitsuka over 9 yearsWhy do it like this... When there's an out of the box System Button?
-
Jayprakash Dubey over 9 years@GabrielTomitsuka : Could you be more elaborative on what you mean?
-
Gabriel Tomitsuka over 9 yearsThere's already an Apple-designed function for doing this; Creating a new type of UIBarButtonItem is unnecessary.