Xcode / Swift: How I implement a back button?

久未见 提交于 2019-12-23 09:40:09

问题


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:

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.


回答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.




回答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.




回答3:


To implement a back button, your root view controller has to be a Navigation Controller.
The first view controller becomes the navigation root of the navigation controller.
If you want to present another view controller, you select a "Show Detail" relationship as the action for the button which should show the view controller. To do this, Ctrl-click and drag from the button to the destination view controller and select "Show Detail".




回答4:


I had the same problem, even when on the storyboard the back button was visible at design time. I deleted the segue, and recreated it with "Show" instead of "Show detail". Changing the segue to "Show" had no effect. I think this is a bug, so if you miss that back button delete and recreate the segue.



来源:https://stackoverflow.com/questions/34404076/xcode-swift-how-i-implement-a-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!