Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring the backBarButtonItem of a View Controller's Navigation Item in a Storyboard

It's easy enough to drag and drop bar button items onto a view controller's navigation bar in a storyboard in Interface Builder. In this way, you can set the leftBarButtonItem and rightBarButtonItem outlets of the view controller's navigation item. But there's also a backBarButtonItem outlet, and it's not obvious at all how to set it. How can I set a custom back bar button item using Interface Builder?

like image 409
warrenm Avatar asked Mar 22 '12 18:03

warrenm


People also ask

How do you add navigation to a storyboard?

Under the View menu, select Utilities→Show Object Library. In the Object Library, find the Navigation Controller object (see Figure 4-7) and drag and drop it into the storyboard, to the left side of your existing view controller (Figure 4-6).

How do I show the back button in Swift?

Back-button text is taken from parent view-controller's navigation item title. So whatever you set on previous view-controller's navigation item title, will be shown on current view controller's back button text. You can just put "" as navigation item title in parent view-controller's viewWillAppear method.


2 Answers

  • Select the view controller whose navigation items you want to change. The black bar displaying the identity of the view controller changes to an iconified tray of its referenced objects.

  • Drag and drop a bar button item from the object library onto the tray.

Storyboard view of a navigation controller and subordinate table view controller

  • Right-click on the view controller's navigation item in the main object tray on the left-hand side. Wire up the newly added button as the navigation item's backBarButtonItem outlet.

Interface Builder screenshot illustrating how to wire up the back bar button outlet

  • Select the bar button and configure it in any way you choose with the Attributes Inspector.

Screenshot of the attributes inspector in Interface Builder showing options for the back bar button item

like image 98
warrenm Avatar answered Nov 09 '22 10:11

warrenm


As @wcochran noted above, when working with viewControllers pushed onto a navigationController's stack, the backBarButtonItem outlet is already wired and can't be changed. Furthermore, selecting the child VC's navigationItem and changing the Back Button text in IB doesn't do what you would expect.

Now you might think that replacing the child VC's backBarButtonItem would solve the problem, but it doesn't. Confusingly, if you want to set the title of the back button of a child VC, you have to set the back button title of its parent (!), like so:

- (void)viewWillAppear:(BOOL)animated // in the parent VC!
{
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButton;
}

This won't do anything on the parent VC. In fact, if the parent is the navigationController's RootViewController, there won't be a back button at all. But the child will inherit (or pick up) the back button you've created.

This only applies to the immediate child VC, so if you want to maintain the label down through the navigationController's stack you need to set it on each parent.

Thanks to @wiliz in #iphonedev for explaining this to me.

like image 22
sumizome Avatar answered Nov 09 '22 08:11

sumizome