简体   繁体   English

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

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

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 是如何工作的以及它们的用途是什么?

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). unwind segue (有时称为exit segue )可用于通过 push、modal 或 popover segue向后导航(就像您从导航栏中弹出导航项、关闭 popover 或关闭以模态呈现的视图控制器一样)。 On top of that you can actually unwind through not only one but a series of push/modal/popover segues, eg "go back" multiple steps in your navigation hierarchy with a single unwind action.最重要的是,您实际上不仅可以通过一个而是通过一系列推送/模态/弹出框来放松,例如,使用单个放松操作“返回”导航层次结构中的多个步骤。

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.当你执行一个unwind segue时,你需要指定一个action,它是你想要展开到的视图控制器的一个action方法。

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.此外,该方法会在执行 unwind segue 之前调用。 You can get the source view controller from the passed UIStoryboardSegue parameter to interact with the view controller that initiated the segue (eg to get the property values of a modal view controller).您可以从传递的UIStoryboardSegue参数中获取源视图控制器,以与启动 segue 的视图控制器进行交互(例如,获取模态视图控制器的属性值)。 In this respect, the method has a similar function as the prepareForSegue: method of UIViewController .在这方面,该方法与UIViewControllerprepareForSegue:方法具有类似的功能。

iOS 8 update: Unwind segues also work with iOS 8's adaptive segues, such as Show and 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:让我们有一个带有导航控制器和三个子视图控制器的故事板:

在此处输入图片说明

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, eg 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.添加 action 方法后,您可以通过按住 Control 拖动到 Exit 图标来定义故事板中的 unwind segue。 Here we want to unwind to Red from Green when the button is pressed:在这里,我们要在按下按钮时从绿色放松到红色:

在此处输入图片说明

You must select the action which is defined in the view controller you want to unwind to:您必须选择要展开的视图控制器中定义的操作:

在此处输入图片说明

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.在执行 unwind segue 之前,将调用 action 方法。 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:我们可以通过 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.展开也可以通过推/模态segue 的组合来工作。 Eg 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:例如,如果我添加了另一个带有模态转场的黄色视图控制器,我们可以一步从黄色返回到红色:

在此处输入图片说明

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

在此处输入图片说明

Selecting the segue and going to the Attributes Inspector reveals the "Identifier" property.选择 segue 并转到 Attributes Inspector 会显示“标识符”属性。 Use this to give a unique identifier to your segue:使用它为您的 segue 提供唯一标识符:

在此处输入图片说明

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)

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 to and 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 从您的按钮或任何东西拖到源视图右上角的橙色小“退出”图标上即可。

在此处输入图片说明

There should now be an option to connect to "- unwindToViewControllerNameHere"现在应该有一个选项可以连接到“- unwindToViewControllerNameHere”

That's it, your segue will unwind when your button is tapped.就是这样,当您的按钮被点击时,您的转场将会放松。

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 MyNavController with A as its root view controller.想象一下,你有一个MyNavController其中A作为它的根视图控制器。 Now you use a push segue to B .现在您对B使用 push segue 。 Now the navigation controller has A and B in its viewControllers array, and B is visible.现在导航控制器的viewControllers数组中有 A 和 B,并且 B 是可见的。 Now you present C modally.现在您以模态呈现C

With unwind segues, you could now unwind "back" from C to B (ie dismissing the modally presented view controller), basically "undoing" the modal segue.使用展开转场,您现在可以从CB展开“返回”(即解除模态呈现的视图控制器),基本上是“撤销”模态转场。 You could even unwind all the way back to the root view controller A , undoing both the modal segue and the push segue.您甚至可以一直解压到根视图控制器A ,同时撤消模态转场和推送转场。

Unwind segues make it easy to backtrack. Unwind segues 使回溯变得容易。 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 .例如,在 iOS 6 之前,关闭呈现的视图控制器的最佳实践是将呈现的视图控制器设置为呈现的视图控制器的委托,然后调用您的自定义委托方法,然后关闭呈现的视图控制器 Sound cumbersome and complicated?听起来繁琐复杂? It was.它是。 That's why unwind segues are nice.这就是为什么 unwind segues 很好。

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.我在这里的其他答案中没有提到的是,当您不知道最初的 segue 起源于何处时如何处理展开,这对我来说是一个更重要的用例。 For example, say you have a help view controller ( H ) that you display modally from two different view controllers ( A and B ):例如,假设您有一个帮助视图控制器( H ),您可以从两个不同的视图控制器( AB )模态显示:

AH AH
BH→H

How do you set up the unwind segue so that you go back to the correct view controller?如何设置 unwind segue 以便返回到正确的视图控制器? The answer is that you declare an unwind action in A and B with the same name , eg:答案是你在AB 中声明了一个同名的 unwind 动作,例如:

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

This way, the unwind will find whichever view controller ( A or 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 的来源,而不是它要去的地方。

Swift iOS:迅捷iOS:

Step 1: define this method into your MASTER controller view.步骤 1:将此方法定义到您的 MASTER 控制器视图中。 in which you want to go back:你想回去的地方:

//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”作为连接您的操作按钮,您将点击该按钮返回到您的主控制器视图:

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

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:

在此处输入图片说明

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM