Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode / Swift: How I implement a back button?

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.

like image 202
Laire Avatar asked Dec 21 '15 20:12

Laire


2 Answers

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.

like image 83
little Avatar answered Nov 15 '22 09:11

little


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.

like image 5
pob21 Avatar answered Nov 15 '22 07:11

pob21