ios Xcode / Swift:我如何实现后退按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34404076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:27:06  来源:igfitidea点击:

Xcode / Swift: How I implement a back button?

iosxcodestoryboardback

提问by Laire

I just started with Xcode and Swift.

我刚开始使用 Xcode 和 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.

我尝试为 iOS 构建我的第一个小应用程序。但是现在我遇到了问题,我不知道如何实现后退按钮,所以我回到了之前的视图。

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.

当我打开 AZ 视图时,我想显示后退箭头,这使我返回到 Item 2 视图。

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

要打开 A - Z 视图,我将按钮“Medikamente A - Z”与导航控制器连接起来。

采纳答案by little

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

使用故事板时,后退按钮通常使用 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

我通常喜欢在 UI 相关主题上关注 raywenderlich toturials,就像这样 - 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.

故事板提供了通过称为 unwind segue 的东西“返回”的能力,接下来您将实现它。主要分为三个步骤: 1. 创建一个供用户选择的对象,通常是一个按钮。2. 在要返回的控制器中创建一个 unwind 方法。3.在storyboard中连接方法和对象。

回答by pob21

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.

使用 UINavigationController 时,每当您推送到新的 ViewController 时,后退按钮都会自动出现,因此您可以跳回上一个 ViewController。

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

所以它的工作原理是: UIViewController -> UIViewController -> UIViewController

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

后退按钮将出现在最后 2 个上,因此您可以弹出前一个 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.

您不必为后退按钮的出现做任何额外的编码,它会自行完成。我希望这可以解决问题。如果您有任何问题,请告诉我。

回答by BlackVoid

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.

我遇到了同样的问题,即使在故事板上后退按钮在设计时也是可见的。我删除了转场,并用“显示”而不是“显示细节”重新创建了它。将 segue 更改为“Show”没有效果。我认为这是一个错误,因此如果您错过了后退按钮,请删除并重新创建转场。

回答by Palle

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".

要实现后退按钮,您的根视图控制器必须是导航控制器。
第一个视图控制器成为导航控制器的导航根。
如果要显示另一个视图控制器,请选择“显示详细信息”关系作为应显示视图控制器的按钮的操作。为此,按住 Ctrl 键单击并从按钮拖动到目标视图控制器,然后选择“显示详细信息”。