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

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

问题描述

iOS 6 和 Xcode 4.5 具有称为Unwind Segue"的新功能:

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

Unwind 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

展开转场的工作原理以及它们的用途是什么?

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

推荐答案

简述

展开转场(有时称为退出转场)可用于通过推送、模态或弹出转场向后导航(就像您从导航中弹出导航项一样栏,关闭弹出窗口或关闭模态呈现的视图控制器).最重要的是,您实际上不仅可以通过一个而是通过一系列推送/模态/弹出框来放松,例如使用单个展开操作返回"导航层次结构中的多个步骤.

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.

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

目标-C:

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

斯威夫特:

@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
}

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

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 更新:Unwind segues 也适用于 iOS 8 的自适应 segues,例如 ShowShow 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:

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

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:

目标-C:

@implementation RedViewController

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

@end

斯威夫特:

@IBAction func unwindToRed(segue: UIStoryboardSegue) {
}

添加动作方法后,您可以通过按住 Control 拖动到退出"图标来定义故事板中的展开转场.这里我们想在按下按钮时从绿色放松到红色:

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.

在执行 unwind segue 之前,调用 action 方法.在这个例子中,我定义了一个从绿色和蓝色到红色的展开转场.我们可以通过 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:

目标-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!");
    }
}

斯威夫特:

@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 也可以通过 push/modal segues 的组合来工作.例如.如果我添加另一个带有模态转场的黄色视图控制器,我们可以一步从黄色返回到红色:

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:

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

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 并转到 Attributes Inspector 会显示标识符"属性.使用它为您的 segue 提供唯一标识符:

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

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

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

目标-C:

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

斯威夫特:

performSegueWithIdentifier("UnwindToRedSegueID", sender: self)

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

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