andig
andig

Reputation: 13906

How to expose standard back button in view controller navigation?

I need users to be able to navigate a data hierarchy (master level, detail level) and to create new master and detail objects accordingly. Both master and detail use arrays for their model and TableViews for presentation

The navigation flow for this uses 2 navigation and table controllers like below. The + of the master and detail TableViews create new objects, the forstTableCell navigates to the second TableView using a segue:

Storyboard

While the screenshot shows "Done" right now even when removing that ButtonItem the slot remains empty.

I would like to show the standard back button instead: "< Middlewares" in this case. In the tests I've only been able to get the back button when navigating to a normal ViewController, but not to another NavigationController. Is it possible to have it between Navigation Controllers, too?

Upvotes: 1

Views: 590

Answers (2)

productioncoder
productioncoder

Reputation: 4335

Simply remove the second navigation controller. If you use a push segue, your second view controller will still have the navigation bar. As long as you don't use a modal segue all view controllers that are pushed will have a navigation bar.

So your storyboard will look like this: Your storyboard

You will then automatically have a back button. If you want to change the text of it, go to your navigation item of your first view controller and change the back title accordingly as shown in this screenshot

enter image description here

You certainly want to have a title in your second view controller (something like "Add [whatever you want to add]". So simply drag & drop a UINavigationItem on your second view controller then you can also add UIBarButton items in your Interface builder

Upvotes: 1

Tommie C.
Tommie C.

Reputation: 13181

Controlling the look/feel of the Navigation Bar Buttons

You can achieve the behavior you want by opening the Document Outline and find the existing Done button. If you have a UIBarButtonItem type, you can simply change the type to Custom in the Inspector. Next add a regular button within the UIBarButtonItem (just bring the Navigation bar for the target controller into the zoomed in view of the storyboard/xib). This will allow you to drag a button onto the navigation bar.

Once you have a standard button you can add an image with the back arrow. Then add supporting code to use the pop behavior on the Navigation bar. Since you can have only one root navigation controller, you may want to remove the second UINavigationController and add a UINavigationItem from the objects library and then subsequently add the buttons, titles of your choice. This configuration will allow you to leverage all of the push and pop methods available, while retaining full control of the look/feel/behavior of the navigation stack.

More on customizing the look/feel/behavior of the UINavigation Stack can be found at: Navigation API Docs

Upvotes: 1

Related Questions