ios 什么是 Unwind segues 以及如何使用它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12561735/
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-30 20:21:40  来源:igfitidea点击:

What are Unwind segues for and how do you use them?

iosios6uistoryboard

提问by Imre Kelényi

iOS 6 and Xcode 4.5 has a new feature referred to as "Unwind Segue":

iOS 6 和 Xcode 4.5 有一个称为“Unwind Segue”的新功能:

Unwind segues can allow transitioning to existing instances of scenes in a storyboard

Unwind segues 可以允许过渡到故事板中的现有场景实例

In addition to this brief entry in Xcode 4.5's release notes, UIViewController now seem to have a couple of new methods:

除了 Xcode 4.5 发行说明中的​​这个简短条目之外, UIViewController 现在似乎有几个新方法:

- (BOOL)canPerformUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender
- (UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier

How do unwind segues work and what they can be used for?

unwind segues 是如何工作的以及它们的用途是什么?

回答by Imre Kelényi

In a Nutshell

简而言之

An unwind segue(sometimes called exit segue) can be used to navigate back through push, modal or popover segues (as if you popped the navigation item from the navigation bar, closed the popover or dismissed the modally presented view controller). On top of that you can actually unwind through not only one but a series of push/modal/popover segues, e.g. "go back" multiple steps in your navigation hierarchy with a single unwind action.

一个退绕SEGUE(有时称为出口SEGUE)可用于通过推,模态或酥料饼塞格斯导航回(如,如果你从导航栏弹出导航项,关闭酥料饼或驳回模态呈现视图控制器)。最重要的是,您实际上不仅可以通过一个而是通过一系列推送/模态/弹出框来放松,例如,使用单个放松操作“返回”导航层次结构中的多个步骤。

When you perform an unwind segue, you need to specify an action, which is an action method of the view controller you want to unwind to.

当您执行开卷SEGUE,你需要指定一个动作,就是要放松到视图控制器的操作方法。

Objective-C:

目标-C:

- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
{
}

Swift:

迅速:

@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
}

The name of this action method is used when you create the unwind segue in the storyboard. Furthermore, this method is called just before the unwind segue is performed. You can get the source view controller from the passed UIStoryboardSegueparameter to interact with the view controller that initiated the segue (e.g. to get the property values of a modal view controller). In this respect, the method has a similar function as the prepareForSegue:method of UIViewController.

在故事板中创建展开转场时使用此操作方法的名称。此外,该方法会在执行 unwind segue 之前调用。您可以从传递的UIStoryboardSegue参数中获取源视图控制器,以与启动 segue 的视图控制器进行交互(例如,获取模态视图控制器的属性值)。在这方面,该方法也有类似功能的prepareForSegue:方法UIViewController

iOS 8 update:Unwind segues also work with iOS 8's adaptive segues, such as Showand Show Detail.

iOS 8 更新:Unwind segues 也适用于 iOS 8 的自适应 segues,例如ShowShow Detail

An Example

一个例子

Let us have a storyboard with a navigation controller and three child view controllers:

让我们有一个带有导航控制器和三个子视图控制器的故事板:

enter image description here

在此处输入图片说明

From Green View Controller you can unwind (navigate back) to Red View Controller. From Blue you can unwind to Green or to Red via Green. To enable unwinding you must add the special action methods to Red and Green, e.g. here is the action method in Red:

从绿色视图控制器,您可以展开(导航回)到红色视图控制器。您可以从蓝色放松到绿色或通过绿色放松到红色。要启用展开,您必须向 Red 和 Green 添加特殊操作方法,例如,这里是 Red 中的操作方法:

Objective-C:

目标-C:

@implementation RedViewController

- (IBAction)unwindToRed:(UIStoryboardSegue *)unwindSegue
{
}

@end

Swift:

迅速:

@IBAction func unwindToRed(segue: UIStoryboardSegue) {
}

After the action method has been added, you can define the unwind segue in the storyboard by control-dragging to the Exit icon. Here we want to unwind to Red from Green when the button is pressed:

添加 action 方法后,您可以通过按住 Control 拖动到 Exit 图标来定义故事板中的 unwind segue。在这里,我们要在按下按钮时从绿色放松到红色:

enter image description here

在此处输入图片说明

You must select the action which is defined in the view controller you want to unwind to:

您必须选择要展开的视图控制器中定义的操作:

enter image description here

在此处输入图片说明

You can also unwind to Red from Blue (which is "two steps away" in the navigation stack). The key is selecting the correct unwind action.

您还可以从蓝色放松到红色(在导航堆栈中“两步之遥”)。关键是选择正确的展开动作。

Before the the unwind segue is performed, the action method is called. In the example I defined an unwind segue to Red from both Green and Blue. We can access the source of the unwind in the action method via the UIStoryboardSegue parameter:

在执行 unwind segue 之前,将调用 action 方法。在这个例子中,我定义了一个从绿色和蓝色到红色的展开转场。我们可以通过 UIStoryboardSegue 参数访问 action 方法中的 unwind 源:

Objective-C:

目标-C:

- (IBAction)unwindToRed:(UIStoryboardSegue *)unwindSegue
{
    UIViewController* sourceViewController = unwindSegue.sourceViewController;

    if ([sourceViewController isKindOfClass:[BlueViewController class]])
    {
        NSLog(@"Coming from BLUE!");
    }
    else if ([sourceViewController isKindOfClass:[GreenViewController class]])
    {
        NSLog(@"Coming from GREEN!");
    }
}

Swift:

迅速:

@IBAction func unwindToRed(unwindSegue: UIStoryboardSegue) {
    if let blueViewController = unwindSegue.sourceViewController as? BlueViewController {
        println("Coming from BLUE")
    }
    else if let redViewController = unwindSegue.sourceViewController as? RedViewController {
        println("Coming from RED")
    }
}

Unwinding also works through a combination of push/modal segues. E.g. if I added another Yellow view controller with a modal segue, we could unwind from Yellow all the way back to Red in a single step:

展开也可以通过推/模态segue 的组合来工作。例如,如果我添加了另一个带有模态转场的黄色视图控制器,我们可以一步从黄色返回到红色:

enter image description here

在此处输入图片说明

Unwinding from Code

从代码中解脱

When you define an unwind segue by control-dragging something to the Exit symbol of a view controller, a new segue appears in the Document Outline:

当您通过控制拖动某些内容到视图控制器的退出符号来定义展开转场时,文档大纲中会出现一个新转场:

enter image description here

在此处输入图片说明

Selecting the segue and going to the Attributes Inspector reveals the "Identifier" property. Use this to give a unique identifier to your segue:

选择 segue 并转到 Attributes Inspector 会显示“标识符”属性。使用它为您的 segue 提供唯一标识符:

enter image description here

在此处输入图片说明

After this, the unwind segue can be performed from code just like any other segue:

在此之后,展开转场可以像任何其他转场一样从代码中执行:

Objective-C:

目标-C:

[self performSegueWithIdentifier:@"UnwindToRedSegueID" sender:self];

Swift:

迅速:

performSegueWithIdentifier("UnwindToRedSegueID", sender: self)

回答by Travis M.

As far as how to use unwind segues in StoryBoard...

至于如何在 StoryBoard 中使用 unwind segues ......

Step 1)

第1步)

Go to the code for the view controller that you wish to unwind toand add this:

转到您希望展开的视图控制器的代码并添加以下内容:

Objective-C

目标-C

- (IBAction)unwindToViewControllerNameHere:(UIStoryboardSegue *)segue {
    //nothing goes here
}

Be sure to also declare this method in your .h file in Obj-C

请务必在 Obj-C 的 .h 文件中声明此方法

Swift

迅速

@IBAction func unwindToViewControllerNameHere(segue: UIStoryboardSegue) {
    //nothing goes here
}

Step 2)

第2步)

In storyboard, go to the view that you want to unwind from and simply drag a segue from your button or whatever up to the little orange "EXIT" icon at the top right of your source view.

在情节提要中,转到您想要从中放松的视图,只需将一个 segue 从您的按钮或任何东西拖到源视图右上角的小橙色“退出”图标上。

enter image description here

在此处输入图片说明

There should now be an option to connect to "- unwindToViewControllerNameHere"

现在应该有一个选项可以连接到“- unwindToViewControllerNameHere”

That's it, your segue will unwind when your button is tapped.

就是这样,当您的按钮被点击时,您的转场将会放松。

回答by Yang Meyer

Unwind segues are used to "go back" to some view controller from which, through a number of segues, you got to the "current" view controller.

Unwind segues 用于“返回”到某个视图控制器,通过一些 segues,您可以从该视图控制器到达“当前”视图控制器。

Imagine you have something a MyNavControllerwith Aas its root view controller. Now you use a push segue to B. Now the navigation controller has A and B in its viewControllersarray, and B is visible. Now you present Cmodally.

想象一下,你有一个MyNavControllerwithA作为它的根视图控制器。现在您使用 push segue 到B. 现在导航控制器的viewControllers数组中有 A 和 B ,并且 B 是可见的。现在您以C模态呈现。

With unwind segues, you could now unwind "back" from Cto B(i.e. dismissing the modally presented view controller), basically "undoing" the modal segue. You could even unwind all the way back to the root view controller A, undoing both the modal segue and the push segue.

使用 unwind segues,您现在可以从Cto 开始“返回” B(即关闭模态呈现的视图控制器),基本上是“撤消”模态 segue。您甚至可以一直放松回到根视图控制器A,同时撤消模态转场和推送转场。

Unwind segues make it easy to backtrack. For example, before iOS 6, the best practice for dismissing presented view controllers was to set the presenting view controller as the presented view controller's delegate, then call your custom delegate method, which then dismisses the presentedViewController. Sound cumbersome and complicated? It was. That's why unwind segues are nice.

Unwind segues 使回溯变得容易。例如,在 iOS 6 之前,关闭呈现的视图控制器的最佳实践是将呈现的视图控制器设置为呈现的视图控制器的委托,然后调用您的自定义委托方法,然后关闭呈现的视图控制器。听起来繁琐复杂?它是。这就是为什么 unwind segues 很好。

回答by shawkinaw

Something that I didn't see mentioned in the other answers here is how you deal with unwinding when you don't know where the initial segue originated, which to me is an even more important use case. For example, say you have a help view controller (H) that you display modally from two different view controllers (Aand B):

我在这里的其他答案中没有提到的是,当您不知道最初的 segue 起源于何处时如何处理展开,这对我来说是一个更重要的用例。例如,假设您有一个帮助视图控制器(H),您可以从两个不同的视图控制器(AB)模态显示:

AH
BH

AH
BH

How do you set up the unwind segue so that you go back to the correct view controller? The answer is that you declare an unwind action in Aand Bwith the same name, e.g.:

你如何设置 unwind segue 以便返回到正确的视图控制器?答案是你在AB 中声明了一个同名的 unwind 动作,例如:

// put in AViewController.swift and BViewController.swift
@IBAction func unwindFromHelp(sender: UIStoryboardSegue) {
    // empty
}

This way, the unwind will find whichever view controller (Aor B) initiated the segue and go back to it.

这样,unwind 将找到启动 segue 的任何视图控制器(AB)并返回到它。

In other words, think of the unwind action as describing where the segue is coming from, rather than where it is going to.

换句话说,将 unwind 操作视为描述 segue 的来源,而不是它要去的地方。

回答by Vinod Joshi

Swift iOS:

迅捷iOS:

Step 1: define this method into your MASTER controller view. in which you want to go back:

步骤 1:将此方法定义到您的 MASTER 控制器视图中。你想回去的地方:

//pragma mark - Unwind Seques
@IBAction func goToSideMenu(segue: UIStoryboardSegue) {

    println("Called goToSideMenu: unwind action")

}

Step 2: (StoryBoard) Right click on you SLAVE/CHILD EXIT button and Select "goToSideMenu" As action to Connect you Button on which you will click to return back to you MASTER controller view:

第 2 步:(故事板)右键单击您的 SLAVE/CHILD EXIT 按钮并选择“goToSideMenu”作为连接您的操作按钮,您将点击该按钮返回到您的主控制器视图:

enter image description herestep 3: Build and Run ...

在此处输入图片说明第 3 步:构建并运行...

回答by Tech

For example if you navigate from viewControllerB to viewControllerA then in your viewControllerA below delegate will call and data will share.

例如,如果您从 viewControllerB 导航到 viewControllerA,那么在您的 viewControllerA 下面的委托将调用并且数据将共享。

@IBAction func unWindSeague (_ sender : UIStoryboardSegue) {
????????if sender.source is ViewControllerB??{
????????????if let _ = sender.source as? ViewControllerB {
????????????????self.textLabel.text = "Came from B = B->A , B exited"
????????????}
????????????
????????}

}
  • Unwind Seague Source View Controller ( You Need to connect Exit Button to VC's exit icon and connect it to unwindseague:
  • Unwind Seague Source View Controller(您需要将退出按钮连接到 VC 的退出图标并将其连接到 unwindseague:

enter image description here

在此处输入图片说明

  • Unwind Seague Completed -> TextLabel of viewControllerA is Changed.
  • Unwind Seague Completed -> viewControllerA 的 TextLabel 已更改。

enter image description here

在此处输入图片说明