0

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 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I see, thanks for the explanation. The "Kind" list in Xcode 10 (segue, attribute view) lists "Push" and a couple of others under "Deprecated Segues", so I'd rather not use it in case it gets removed in the near future. Plus, the app crashed when I tried to use it. V1 is the login screen and is displayed only once but I want to switch between v2 and v3 often and quickly. I'm okay with using a BarButtonItem and "Show", I was just wondering if there still was an automatic way before I set everything up and if there's going to be a problem with performance if I load/unload the views often.
What are the "Back" attributes even used for (Navigation bar & Navigation bar text)?
Sorry, another comment: I just tested it with a BarButtonItem that's connected to v2 via a "show" segue and the ListView in v2 is empty. V1 gives v2 an array of items to fill the list with but v3 doesn't do that of course and it looks like a "Show v3" completely destroys v2. So in the end I do need something that only hides v2 and doesn't destroy it, so I don't have to set up everything in v2 again. Edit: Looks like "Push" has been deprecated since iOS8 (I'm using iOS12).
I see. So v1(login screen) can be a view controller on its own, and if it is does not need to be in a navigation then exclude it. And instead of "show (e.g. push)" I would set the segue from v1 to v2's navigation controller to "present modally. Make sure v2 is in a navigation controller. Then create another segue from v2 to v3 using "show (e.g. push) ". Once the item in the list in v2 is selected, perform that segue you just created. In v3 the back button to v2 will automatically appear. The key is not to leave the navigation controller.
There is no back attribute when you create a custom bar item button, you mostly like have to dismiss a view or rewind to the previous view. For performance wise, keep it in the navigation controller. I can create a sample project for you if you need further help.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.