0
votes

First of all: I'm using Swift 5 with Xcode 10 (iOS 12.0).

I'm pretty new to Swift/iOS development and watched a couple of videos on how to add a "back" button to the navigation bar. In all of them (with Swift 3) the button just magically appears once the app is run after adding a segue (between an item in the first scene and the second scene) and a title for both navigation bars.

My app has three ViewControllers/scenes:

  1. Login without navigation bar ("login" button: segue to scene 2)
  2. Table View (linked to scene 3 with segue, so I can just tap on an item in the list)
  3. Further information for a single item in the Table View

The segue between scene 2 and 3 is set to "show (e.g. push)" because "push" is deprecated and crashes the app.

I added both navigation bars to the navigation controllers (2 & 3) by hand and want to add an arrow icon to the navigation bar of scene 3. What I've tried so far:

  • Add titles for the navigation bar in both scenes
  • Set the "Back button" attribute (Navigation bar text), which created a child object
  • Then set the child object's "Image" attribute
  • Set the "Back" and "Back Mask" attribute (Navigation bar) to the same image

Nothing shows up though, neither in Xcode nor in the simulator (not even the image).

Is it even still possible to get an automatic "back" button or do you now have to add it yourself using a "Bar Button Item"?

2

2 Answers

0
votes

So when you show a View Controller such as your v2,v3 those View Controllers are no longer in the same navigation stack as your v1, that is why it is not automatically showing the back button.

I don't know why it is saying your Push function is deprecated but if you could grab a screenshot maybe I can give you a reason why. I am using Swift5 and I'm able to use this : self.navigationController?.pushViewController(vc, animated: true)

To answer your question, if you want to add a back button to v2,v3 then you DO have to use BarButtonItem, and call self.dismiss(viewController, true) I strongly recommend you not do it this way if your intent is to have them in the same Navigation stack as your v1.

0
votes

Answering my own question: Yes, it's still (Xcode 10/Swift 5) possible to make it add a "back" button automatically.

Push segues are deprecated since iOS 8, so use Show (e.g. Push) instead!

Usually a Show (e.g. Push) segue uses Show but if there's a NavigationController somewhere to the left (scenes going from left to right) of the current ViewController, it automatically uses Push, which also adds a "back" button to the NavigationBar of the next scene or, if there's no NavigationBar, it simply adds the button where the bar would usually be.

My scenes now look like this:

  1. ViewController1: Login without NavigationBar ("login" button: segue to NavigationController)
  2. NavigationController: With automatic segue to a new UITableViewController (if added through the library)
  3. ViewController2: Table View (linked to 4. with segue)
  4. ViewController3: Further information for a single item

Adding data to the segues between 1. and 3. (using 2.) is handled a little bit different than it would be without NavigationController:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let navC = segue.destination as? UINavigationController
    let vc2 = navC?.viewControllers.first as! ViewController2
    vc2.myinformation = myinfosArray
}

(Bad) Alternative to a NavigationController:

Add a NavigationBar and a BarButtonItem and link it to the previous view like you would any other button.

Disadvantage: The view (in my case "ViewController2") resets once you switch to the next one (because the two views aren't on the same stack, see jaelee's explanation above), so if you go back you have to set everything up again.