Xcode / Swift: How I implement a back button?

40,221

Solution 1

When using storyboards the back button is usually implemented with unwind segue.

I usually like to follow raywenderlich toturials on UI related topics, like this - http://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2

It include a detailed example of how to implement back button in storyboards. Quoting from it -

Storyboards provide the ability to ‘go back’ with something called an unwind segue, which you’ll implement next. There are three main steps: 1. Create an object for the user to select, usually a button. 2. Create an unwind method in the controller that you want to return to. 3. Hook up the method and the object in the storyboard.

Solution 2

When using UINavigationController, whenever you push to a new ViewController the back button will automatically appear, so you can jump back to the previous View Controller.

So it's works like: UIViewController -> UIViewController -> UIViewController

A back button will appear on the last 2 so you can pop back the the previous ViewController.

You don't have to do any additional coding for the back button to appear, it'll do it on its own. I hope this clears it up. Let me know if you have any questions.

Share:
40,221
Laire
Author by

Laire

Updated on July 09, 2022

Comments

  • Laire
    Laire almost 2 years

    I just started with Xcode and Swift.

    I try to build my first little App for iOS. But now I have the problem, that I don't know how to implement a the back button, so that i come back to the view before.

    My Storyboard look like this:

    Storyboard

    When I open the A-Z view, I want to display the Back Arrow, which turn me back to the Item 2 view.

    To open the A - Z view I connect the button "Medikamente A - Z" with the Navigation Controller.