How do I get the navigation controller to show the back button?

How do I get the navigation controller to show the back button on the navigation bar of the root view controller? When it does the navigation bar shows a left arrow on the left-hand side with a title after it.

Accepted Reply

I've looked back at some of my old projects.


It seems that the back button is (was ?) automatically inserted when navigation came from a table view cell. The back button does not show in storyboard, but is inserted automatically in the navigation bar, with the name of the view you came from.


The complete set up was:

- a navigation controller linked to a tableview controller (created altogether in IB when creating a navigation controller)

- the link between the 2 is a relationship type (drawn as is a circle with a diagonal line (top left to bottom right) ending with two endpoints)

- the protoype cell is connected to another tableView


Hope that helps.

Replies

Can you explain the set up ?

You have a root view controller.

Do you segue to another viewController ?


Why do you ant a back button on rootViewController ? Back to what ?

I'm sure I remember that when I segue to a view controller that is a root view controller of a navigation controller that the navigation bar in the view controller automatically has a back button on the left-hand side pointing back to the original view controller I segued from. I have seen this in an older version of the FoodTracker tutorial project. I thought this is true when I use a show segue.

You just need to add a Bar Button Item inside the Navigation Bar (at the left)


include this in the original controller :

@IBAction func unwindToViewController(_ sender: UIStoryboardSegue) {
}


No code inside. It's just to create a link to the first controller.

And then connect this button to the exit button of its controller, selecting unwindToViewController in the popup.

You can add print("I returned")

just to see it working.


@IBAction func unwindToViewController(_ sender: UIStoryboardSegue) {
     print("I returned")
}



Some more details here, with a slighly different way of doing (but not with a button in tab bar)

h ttps://medium.com/@mimicatcodes/create-unwind-segues-in-swift-3-8793f7d23c6f

I've done that before. I was sure that the FoodTracker turorial showed how to have the navigation controller automatically show a back button by only creating a segue by control-dragging. I could be wrong.

I think I remember the same thing, automatic creation of the back button, but could not reproduce in XCode 9. But could not find example of it in older projects.

Oh well. I guess that's that.

I've looked back at some of my old projects.


It seems that the back button is (was ?) automatically inserted when navigation came from a table view cell. The back button does not show in storyboard, but is inserted automatically in the navigation bar, with the name of the view you came from.


The complete set up was:

- a navigation controller linked to a tableview controller (created altogether in IB when creating a navigation controller)

- the link between the 2 is a relationship type (drawn as is a circle with a diagonal line (top left to bottom right) ending with two endpoints)

- the protoype cell is connected to another tableView


Hope that helps.

Thanks a bunch for looking into that for me!