Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a UIBarButtonItem programmatically to UINavigationBar

I dropped in a UINavigationBar in UIInterfaceBuilder. I present this view modally and just want a UIBackBarButton to return to my last view. I have an outlet and property to this UINavigationBar declared. I thought in my viewDidLoad method, I could create a UIBackButton 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 the UINavigationBar. I think I am doing something wrong here since I don't think my UINavigationBar knows I'm trying to add this UIBackBarButtonItem to it in this way. Would I have to do create an NSArray, 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!

like image 396
Crystal Avatar asked Jul 17 '11 00:07

Crystal


People also ask

How do I add a left bar button in Swift?

You need to open the storyboard, delete the view controller that you have, press cmd , shift , l , and then search for navigation controller . Drag that onto the storyboard. You now need to click on the navigation controller and set it to be the is initial view controller under the attributes inspector .

How do I create a navigation bar in Swift?

Start with Navigation ControllerCreate a single view application in Xcode. Add two view controller into your storyboard. Create two different swift files for those view controllers and set identifiers for them. Take a button in each view controller, set constrain for them and customize as you want.


2 Answers

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;
like image 100
Suhail Patel Avatar answered Nov 15 '22 14:11

Suhail Patel


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.

like image 35
Rahul Sharma Avatar answered Nov 15 '22 15:11

Rahul Sharma