本文翻译自: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: 答案是,您在A和B 中用相同的名称声明一个展开动作,例如:

// 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的来源 ,而不是去向。

什么是Unwind segues,您如何使用它们?相关推荐

  1. iOS Storyboard unwind segues使用小结

    使用storyboard开发的时候,经常会在一个scene上添加一个button,再拖拽这个button到某个想要关联的页面,最后选择push的方式跳转.这样scene_A和scene_B就有了一个& ...

  2. ios开发-Storyboard在多个viewcontroller之间导航的实现

    IOS SDK6/Xcode4.5开始在Storyboad中新增很多功能对可视化的开发页面布局,导航更加方便,下面就写一下各种导航的实现. 1.不用像Xcode4之前必须删除默认的viewcontro ...

  3. xcode 4.5 new feature __ ios6 新特性

    上两周看了wwdc 2012 developer session 400 - 412的视频,以下总结一下xcode4.5的新特性.(部分參考onevcat的文章,在此感谢.) @synthesize ...

  4. iOS 苹果官方Demo

    GitHub 文章链接地址, 欢迎Star+Fork Mirror of Apple's iOS samples This repository mirrors Apple's iOS samples ...

  5. iOS 苹果官方Demo合集

    转自:https://github.com/WildDylan/appleSample Mirror of Apple's iOS samples This repository mirrors Ap ...

  6. 0306--iOS之阅读View Controller Programming Guide for iOS---(三)Presentations and Trasitions

    Presenting a View Controller                                                 --显示vc There are two wa ...

  7. iOS 8 Handoff 开发指南

    (原文:Working with Handoff in iOS 8 作者:Gabriel Theodoropoulos 译者:半圆圆) 我想用下面这一个美妙的场景来开始这篇教程:假象一下你正在Mac上 ...

  8. 多台设备同步 NSUserActivity详解

    API介绍 我们看NSUserActivity的官方文档,可以看到下边的这些API: /* NSUserActivity encapsulates the state of a user activi ...

  9. iOS 各版本中的新特性(What's New in iOS)- 目录翻译完成

    iOS 各版本中的新特性(What's New in iOS) 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&q ...

最新文章

  1. 如何用 Redis 解决海量重复提交问题
  2. [javaSE] 标识符大小写
  3. 云服务器系统租赁费用,云服务器创建租赁费用
  4. Android 高级编程【6个实战案例(附源码):刮刮卡、补间动画、逐帧动画、Fragment、RecyclerView、下拉刷新】
  5. opencv学习笔记8:类型转换
  6. 【Linux】一步一步学Linux——chage命令(92)
  7. 数据分析机器学习-分类好坏的评价方式
  8. ActivityGroup 实现分页和自定义标签(内有GridView的点击背景样式的改变方法)
  9. SAP Spartacus的产品主数据模型
  10. ThoughtWorks洞见领域驱动设计思维导图笔记
  11. IDEA这样配置注释模板,让你高出一个逼格!!
  12. 关于决策树,你一定要知道的知识点!
  13. GraphQL:现代数据库管理系统的演变
  14. matlab打开figure2,matlab figure2无法输出图形
  15. 用curl发起https请求
  16. grafna监控Java接口_性能测试之 JVM 的监控 Grafana
  17. Windows Server 2016安装SQLServer2008R2
  18. MATLAB函数step()对单位负反馈系统求阶跃响应
  19. 河北单招2021计算机类,2021河北省单招十大类专业
  20. 2013应届毕业生“艺龙旅行网”校招应聘总结

热门文章

  1. Android架构师亲述:我从某度外包到字节,你知道我经历了什么吗?
  2. CUSTOMDRAW msdn网站
  3. Android StateListDrawable的坑
  4. 您的手机上未安装应用程序 android 点击快捷方式提示未安装程序的解决
  5. 读书笔记-《增长黑客》-搭建增长团队
  6. 人工智能实践:TensorFlow笔记学习(六)—— 全连接网络实践
  7. 神盾局hive是什么_《神盾局特工》第七季再爆幕后照,这位角色复古装又美出新高度...
  8. nginx 一个请求发给多台机器_Nginx系列二:负载均衡与反向代理
  9. 布尔类型和三目运算符
  10. LintCode-56.两数之和