Configuring the backBarButton element of the view controller navigation element in the storyboard

It’s easy enough to drag the elements of the bar panel onto the navigation bar of the view controller in the storyboard in Interface Builder. That way, you can set the leftBarButtonItem and rightBarButtonItem outputs of the view controller navigation element. But there is an output backBarButtonItem , and it is not clear how to install it. How to customize a control panel button item using Interface Builder?

+7
source share
4 answers
  • Select the view controller whose navigation elements you want to change. The black bar displaying the identifier of the view controller changes to the highlighted tray of its reference objects.

  • Drag a toolbar item from the object library to the tray.

Storyboard view of a navigation controller and subordinate table view controller

  • Right-click the view controller navigation item in the main tray of the object on the left side. Connect the newly added button as the navigation item backBarButtonItem .

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

  • Select the panel button and configure it in any way using the Attributes Inspector.

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

+18
source

As noted above, @wcochran, when working with viewControllers pushed onto the navigation controller stack, the backBarButtonItem output is already connected and cannot be changed. In addition, selecting a child VC navigation element and changing the text of the back button in IB does not do what you expect.

Now you might think that replacing the child VC backBarButtonItem will solve the problem, but it is not. Confusingly, if you want to set the title of the back button of a child VC, you must set the title of the button back to your parent (!), For example:

 - (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 will not do anything on the parent VC. In fact, if the parent is the RootViewController navigationController, there will be no back button at all. But the child inherits (or takes) the button you created back.

This only applies to the immediate child VC, so if you want to keep the shortcut down through the navigationController stack, you need to set it for each parent.

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

+16
source

As noted above, as @AdamBlock, you must install everything correctly in the parent VC.

It shows how to do this programmatically. It is also possible to do this in the interface constructor.

  • Choose parent VC
  • Select a navigation item
  • Open Attribute Inspector
  • Set a title for the back button.
+3
source

In the Builder interface, you can change the title of the Navigation Element button.

Programmatically, you can set a custom return button in viewDidLoad view controller mode. In this example, we set the button image to an image named "customImage.png":

 - (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStyleBordered target:self action:nil]; // Set custom image here backButton.image = [UIImage imageNamed:@"customImage.png"]; self.navigationItem.backBarButtonItem = backButton; } 
+1
source

All Articles