什么是Unwind segues以及如何使用它们? [英] What are Unwind segues for and how do you use them?

查看:151
本文介绍了什么是Unwind segues以及如何使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


展开segues可以允许转换到现有实例故事板中的场景

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

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

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

- (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

unwind segue如何工作以及它们可用于什么?

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

推荐答案

在坚果壳中



展开segue (有时称为退出segue )可用于导航回推送,模态或弹出窗口(如果您从导航栏弹出导航项,关闭弹出窗口或关闭模态显示的视图控制器)。除此之外,您实际上不仅可以通过一个推送/模态/弹出窗口来展开,例如,使用单个展开操作返回导航层次结构中的多个步骤。

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时,需要指定一个动作,这是一个动作方法要放松的视图控制器。

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:

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

Swift:

@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
}

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

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

iOS 8更新:展开segues也适用于iOS 8的自适应segue,例如 Show Show Detail

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

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

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, e.g. here is the action method in Red:

Objective-C:

@implementation RedViewController

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

@end

Swift:

@IBAction func unwindToRed(segue: UIStoryboardSegue) {
}

添加操作方法后,您可以通过控制拖动到退出图标来定义故事板中的展开segue。这里我们想要在按下按钮时从绿色中取消红色:

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:

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

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.

在执行展开segue之前,将调用action方法。在示例中,我从Green和Blue中定义了一个展开为红色的segue。我们可以通过UIStoryboardSegue参数访问action方法中的unwind源:

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:

Objective-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")
    }
}

展开也可以通过推/模态段的组合来实现。例如。如果我添加了另一个带有模态segue的黄色视图控制器,我们可以在一步中从黄色一直放回到红色:

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时,文档大纲中会出现一个新的segue:

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:

选择segue并继续到属性检查器显示标识符属性。使用此标识为您的segue提供唯一标识符:

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

此后,可以像其他任何segue一样从代码执行展开segue:

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

Objective-C:

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

Swift:

performSegueWithIdentifier("UnwindToRedSegueID", sender: self)

这篇关于什么是Unwind segues以及如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆