iOS 6 and Xcode 4.5 has a new feature referred to as "Unwind Segue":
ios6和Xcode 4.5有一个新特性称为“Unwind Segue”:
Unwind segues can allow transitioning to existing instances of scenes in a storyboard
Unwind segue可以允许在故事板中转换到现有的场景实例。
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 segue如何工作以及它们可以用来做什么?
6 个解决方案
#1
1212
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(有时称为exit segue)可以用来通过push、modal或popover segue(就像你从导航栏弹出导航项,关闭弹出窗口或取消modally呈现的视图控制器)。除此之外,你还可以通过不止一个的push/modal/ popoversegues来unwind。在导航层次结构中使用单个unwind动作“返回”多个步骤。
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时,您需要指定一个动作,这是您想要unwind的视图控制器的操作方法。
Objective-C:
objective - 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 (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
.
当您在故事板中创建unwind segue时,将使用此操作方法的名称。此外,在执行unwind segue之前调用此方法。您可以通过传递的UIStoryboardSegue参数获取源视图控制器,以与发起segue的视图控制器交互(例如获取模式视图控制器的属性值)。在这方面,该方法具有类似prepareForSegue: UIViewController方法的功能。
iOS 8 update: Unwind segues also work with iOS 8's adaptive segues, such as Show and Show Detail.
iOS 8更新:Unwind segue也适用于iOS 8的自适应segue,如Show和Show 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, e.g. here is the action method in Red:
从绿色视图控制器,您可以unwind(返回)到红色视图控制器。从蓝色你可以unwind到绿色或红色通过绿色。为了启用unwind,您必须将特殊的操作方法添加到红色和绿色中,例如:这里是红色的操作方法:
Objective-C:
objective - 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:
添加了action方法之后,您可以通过control-拖动到退出图标来在故事板中定义unwind segue。当按钮被按下时,我们想从绿色unwind到红色。
You must select the action which is defined in the view controller you want to unwind to:
您必须选择要unwind到的视图控制器中定义的操作:
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.
您还可以从Blue(导航堆栈中的“两个步骤”)中unwind到Red。关键是选择正确的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:
在执行unwind segue之前,调用操作方法。在这个例子中,我定义了一个从绿色和蓝色的unwind segue到红色。我们可以通过UIStoryboardSegue参数访问操作方法中unwind的源:
Objective-C:
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")
}
}
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:
unwind也可以通过push/modal segue的组合进行。如果我用一个modal segue添加另一个黄色的视图控制器,我们可以在一个步骤中从黄色一直到红色。
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:
当您通过control拖动某些东西到视图控制器的退出符号来定义unwind 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:
在此之后,unwind segue可以像任何其他segue一样从代码执行:
Objective-C:
objective - c:
[self performSegueWithIdentifier:@"UnwindToRedSegueID" sender:self];
Swift:
迅速:
performSegueWithIdentifier("UnwindToRedSegueID", sender: self)
#2
151
As far as how to use unwind segues in StoryBoard...
至于如何在故事板中使用unwind segue…
Step 1)
步骤1)
Go to the code for the view controller that you wish to unwind to and add this:
转到您希望unwind的视图控制器的代码,并添加以下内容:
Objective-C
objective - c
- (IBAction)unwindToViewControllerNameHere:(UIStoryboardSegue *)segue {
//nothing goes here
}
Be sure to also declare this method in your .h file in Obj-C
在objc - 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.
在故事板中,转到你想要unwind的视图,简单地从你的按钮上拖拽一个segue,或者在你的source视图的右上角的那个橙色的“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会在你的按钮被点击的时候unwind。
#3
43
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. Now you use a push segue to B
. Now the navigation controller has A and B in its viewControllers
array, and B is visible. Now you present C
modally.
假设你有一个MyNavController作为它的根视图控制器。现在你使用push segue到B,现在导航控制器在viewControllers数组中有a和B, B是可见的。现在你用的是C模式。
With unwind segues, you could now unwind "back" from C
to B
(i.e. dismissing the modally presented view controller), basically "undoing" the modal segue. You could even unwind all the way back to the root view controller A
, undoing both the modal segue and the push segue.
使用unwind segue,您现在可以从C到B(即解散modally呈现的视图控制器),基本上“撤消”modal segue。你甚至可以unwind回根视图控制器A,取消模态segue和push segue。
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. Sound cumbersome and complicated? It was. That’s why unwind segues are nice.
Unwind segue使回溯变得容易。例如,在ios6之前,取消present视图控制器的最佳实践是将present视图控制器设置为present视图控制器的委托,然后调用您的自定义委托方法,然后取消presentedViewController。听起来繁琐和复杂的吗?这是。这就是unwind segue很不错的原因。
#4
29
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):
我在其他答案中没有提到的是当你不知道初始segue的起源时如何处理unwind,这对我来说是一个更重要的用例。例如,假设您有一个帮助视图控制器(H),您可以从两个不同的视图控制器(a和B)中显示modally:
A → H
B → H
→H B→H
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, e.g.:
如何设置unwind segue以便返回到正确的视图控制器?答案是,在A和B中声明一个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将会找到任何一个视图控制器(A或B)启动segue并返回到它。
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从哪里来的,而不是它将要到哪里。
#5
20
Swift iOS:
斯威夫特iOS:
Step 1: define this method into your MASTER controller view. in which you want to go back:
步骤1:将此方法定义为您的主控制器视图。你想回去:
//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:(故事板)右键单击你的“从/子”退出按钮,选择“goToSideMenu”作为连接你的按钮的动作,你将点击返回到你的主控制器视图:
step 3: Build and Run ...
步骤3:构建和运行…
#6
1
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源视图控制器(你需要将退出按钮连接到VC的退出图标,并将其连接到unwindseague:
- Unwind Seague Completed -> TextLabel of viewControllerA is Changed.
- Unwind Seague完成- viewControllerA的> TextLabel更改。
#1
1212
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(有时称为exit segue)可以用来通过push、modal或popover segue(就像你从导航栏弹出导航项,关闭弹出窗口或取消modally呈现的视图控制器)。除此之外,你还可以通过不止一个的push/modal/ popoversegues来unwind。在导航层次结构中使用单个unwind动作“返回”多个步骤。
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时,您需要指定一个动作,这是您想要unwind的视图控制器的操作方法。
Objective-C:
objective - 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 (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
.
当您在故事板中创建unwind segue时,将使用此操作方法的名称。此外,在执行unwind segue之前调用此方法。您可以通过传递的UIStoryboardSegue参数获取源视图控制器,以与发起segue的视图控制器交互(例如获取模式视图控制器的属性值)。在这方面,该方法具有类似prepareForSegue: UIViewController方法的功能。
iOS 8 update: Unwind segues also work with iOS 8's adaptive segues, such as Show and Show Detail.
iOS 8更新:Unwind segue也适用于iOS 8的自适应segue,如Show和Show 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, e.g. here is the action method in Red:
从绿色视图控制器,您可以unwind(返回)到红色视图控制器。从蓝色你可以unwind到绿色或红色通过绿色。为了启用unwind,您必须将特殊的操作方法添加到红色和绿色中,例如:这里是红色的操作方法:
Objective-C:
objective - 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:
添加了action方法之后,您可以通过control-拖动到退出图标来在故事板中定义unwind segue。当按钮被按下时,我们想从绿色unwind到红色。
You must select the action which is defined in the view controller you want to unwind to:
您必须选择要unwind到的视图控制器中定义的操作:
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.
您还可以从Blue(导航堆栈中的“两个步骤”)中unwind到Red。关键是选择正确的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:
在执行unwind segue之前,调用操作方法。在这个例子中,我定义了一个从绿色和蓝色的unwind segue到红色。我们可以通过UIStoryboardSegue参数访问操作方法中unwind的源:
Objective-C:
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")
}
}
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:
unwind也可以通过push/modal segue的组合进行。如果我用一个modal segue添加另一个黄色的视图控制器,我们可以在一个步骤中从黄色一直到红色。
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:
当您通过control拖动某些东西到视图控制器的退出符号来定义unwind 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:
在此之后,unwind segue可以像任何其他segue一样从代码执行:
Objective-C:
objective - c:
[self performSegueWithIdentifier:@"UnwindToRedSegueID" sender:self];
Swift:
迅速:
performSegueWithIdentifier("UnwindToRedSegueID", sender: self)
#2
151
As far as how to use unwind segues in StoryBoard...
至于如何在故事板中使用unwind segue…
Step 1)
步骤1)
Go to the code for the view controller that you wish to unwind to and add this:
转到您希望unwind的视图控制器的代码,并添加以下内容:
Objective-C
objective - c
- (IBAction)unwindToViewControllerNameHere:(UIStoryboardSegue *)segue {
//nothing goes here
}
Be sure to also declare this method in your .h file in Obj-C
在objc - 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.
在故事板中,转到你想要unwind的视图,简单地从你的按钮上拖拽一个segue,或者在你的source视图的右上角的那个橙色的“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会在你的按钮被点击的时候unwind。
#3
43
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. Now you use a push segue to B
. Now the navigation controller has A and B in its viewControllers
array, and B is visible. Now you present C
modally.
假设你有一个MyNavController作为它的根视图控制器。现在你使用push segue到B,现在导航控制器在viewControllers数组中有a和B, B是可见的。现在你用的是C模式。
With unwind segues, you could now unwind "back" from C
to B
(i.e. dismissing the modally presented view controller), basically "undoing" the modal segue. You could even unwind all the way back to the root view controller A
, undoing both the modal segue and the push segue.
使用unwind segue,您现在可以从C到B(即解散modally呈现的视图控制器),基本上“撤消”modal segue。你甚至可以unwind回根视图控制器A,取消模态segue和push segue。
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. Sound cumbersome and complicated? It was. That’s why unwind segues are nice.
Unwind segue使回溯变得容易。例如,在ios6之前,取消present视图控制器的最佳实践是将present视图控制器设置为present视图控制器的委托,然后调用您的自定义委托方法,然后取消presentedViewController。听起来繁琐和复杂的吗?这是。这就是unwind segue很不错的原因。
#4
29
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):
我在其他答案中没有提到的是当你不知道初始segue的起源时如何处理unwind,这对我来说是一个更重要的用例。例如,假设您有一个帮助视图控制器(H),您可以从两个不同的视图控制器(a和B)中显示modally:
A → H
B → H
→H B→H
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, e.g.:
如何设置unwind segue以便返回到正确的视图控制器?答案是,在A和B中声明一个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将会找到任何一个视图控制器(A或B)启动segue并返回到它。
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从哪里来的,而不是它将要到哪里。
#5
20
Swift iOS:
斯威夫特iOS:
Step 1: define this method into your MASTER controller view. in which you want to go back:
步骤1:将此方法定义为您的主控制器视图。你想回去:
//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:(故事板)右键单击你的“从/子”退出按钮,选择“goToSideMenu”作为连接你的按钮的动作,你将点击返回到你的主控制器视图:
step 3: Build and Run ...
步骤3:构建和运行…
#6
1
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源视图控制器(你需要将退出按钮连接到VC的退出图标,并将其连接到unwindseague:
- Unwind Seague Completed -> TextLabel of viewControllerA is Changed.
- Unwind Seague完成- viewControllerA的> TextLabel更改。