什么是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 放开序列可以允许过渡到情节提要中场景的现有实例

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? 放松搜寻的工作方式以及它们的用途是什么?


#1楼

参考:https://stackoom.com/question/qhsd/什么是Unwind-segues-您如何使用它们


#2楼

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 Segue用于“返回”某个视图控制器,通过一系列的Segue,您可以从中获得“当前”视图控制器。

Imagine you have something a MyNavController with A as its root view controller. 假设您有一个MyNavController ,它的根视图控制器为A Now you use a push segue to B . 现在,您可以对B进行推送。 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. 有了解散键,您现在可以从C退回至B (即解散模态呈现的视图控制器),基本上是“撤消”模态键。 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. 轻松进行搜寻可以轻松回溯。 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之前,关闭展示的视图控制器的最佳实践是将展示的视图控制器设置为展示的视图控制器的委托,然后调用自定义委托方法,然后关闭presentedViewController Sound cumbersome and complicated? 听起来麻烦又复杂? It was. 它是。 That's why unwind segues are nice. 这就是为什么放松的感觉很好。


#3楼

As far as how to use unwind segues in StoryBoard... 至于如何在StoryBoard中使用放松进度...

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. 在情节提要中,转到要放松的视图,只需将按钮或任何东西拖到源视图右上角的橙色小“ EXIT”图标上即可。

在此处输入图片说明

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

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


#4楼

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). 展开序列 (有时称为exit segue )可用于在推送,模态或弹出窗口序列中导航(就像您从导航栏中弹出导航项,关闭弹出窗口或关闭模态显示的视图控制器一样)。 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. 当执行展开选择时,需要指定一个动作,这是您要展开到的视图控制器的动作方法。

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 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. 您可以从Green View Controller展开(向后导航)到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: 要启用展开,必须将特殊的操作方法添加到红色和绿色,例如,这是红色中的操作方法:

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: 在这里,我们想在按下按钮时从绿色退到红色:

在此处输入图片说明

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. 在执行展开搜索之前,将调用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方法中访问展开源:

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. 放卷还可以通过推送/模式搜索结合使用。 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: 例如,如果我添加了另一个带有模态搜索的Yellow视图控制器,我们可以在一个步骤中从Yellow一直退回到Red:

在此处输入图片说明

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并转到“属性”检查器,将显示“标识符”属性。 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: 之后,可以像其他任何segue一样从代码执行展开segue:

Objective-C: 目标C:

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

Swift: 迅速:

performSegueWithIdentifier("UnwindToRedSegueID", sender: self)

#5楼

Swift iOS: Swift 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:(StoryBoard)右键单击SLAVE / CHILD EXIT按钮,然后选择“ goToSideMenu”作为“连接您”按钮的操作,您将单击该按钮以返回到主控制器视图:

在此处输入图片说明step 3: Build and Run ... 步骤3:建立并执行...


#6楼

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 ( A and B ): 例如,假设您有一个帮助视图控制器( H ),可以从两个不同的视图控制器( AB )模态显示:

AH AH
BH 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 A and B with the same name , eg: 答案是,您在AB 中用相同的名称声明一个展开动作,例如:

// 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. 通过这种方式,展开将找到启动视图的任何视图控制器( AB )并返回。

In other words, think of the unwind action as describing where the segue is coming from , rather than where it is going to. 换句话说,将放松动作视为描述segue的来源 ,而不是去向。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值