在 iOS 5 上使用 Segues/Storyboard 弹出当前视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11035809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pop the current view using Segues/Storyboard on iOS 5
提问by Jukurrpa
I am creating an app using iOS 5 SDK. I managed to push views using the Storyboard's Segues, but I cannot find the proper way to pop the current view and go back to the previous one.
我正在使用 iOS 5 SDK 创建一个应用程序。我设法使用 Storyboard 的 Segue 推送视图,但我找不到弹出当前视图并返回到前一个视图的正确方法。
I am not using any navigationController (the app doesn't have any top or bottom bars).
我没有使用任何导航控制器(该应用程序没有任何顶部或底部栏)。
I don't think using modal or push segue the other way would be the solution as it instantiates a new controller.
我不认为使用 modal 或 push segue 的另一种方式是解决方案,因为它实例化了一个新的控制器。
Do I have to use a custom Segue with the opposite animation and deletion of the view at the end ? Or is there a better way ?
我是否必须使用带有相反动画的自定义 Segue 并在最后删除视图?或者,还有更好的方法 ?
回答by rickster
Storyboards in iOS 5 don't provide a "no-code" way to return from a segue -- that's something you'll need to implement yourself.
iOS 5 中的 Storyboard 不提供从 segue 返回的“无代码”方式——这是您需要自己实现的东西。
If you use "push" segues (which require a navigation controller), use the navigation controller's popViewControllerAnimated:
method to undo the last push segue. (Or other methods to undo more; see the UINavigationController
documentation.)
如果您使用“推送”转场(需要导航控制器),请使用导航控制器的popViewControllerAnimated:
方法撤消最后一次推送转场。(或其他撤消更多的方法;请参阅UINavigationController
文档。)
If you use "modal" segues, call dismissViewControllerAnimated:completion:
on the view controller which presented the current view controller (which you can get from its presentingViewController
property).
如果您使用“模态”转场,请调用dismissViewControllerAnimated:completion:
呈现当前视图控制器的视图控制器(您可以从其presentingViewController
属性中获取)。
Update:In iOS 6 and later there's unwind segues for going "back" in a storyboard. It's still not a no-code solution -- and it shouldn't be, because you need to be able to do things like differentiating between "Done" and "Cancel" exits from a modal view controller. But it does let you put more of the semantic flow of your app into the storyboard. Apple has a tech note that describes them in detail, and they're also covered in the video from WWDC 2012 Session 407.
更新:在 iOS 6 及更高版本中,有用于在情节提要中“返回”的 unwind segues。它仍然不是一个无代码解决方案——它不应该是,因为你需要能够做一些事情,比如区分“完成”和“取消”从模态视图控制器退出。但它确实让您将应用程序的更多语义流放入故事板中。Apple 有一份技术说明详细描述了它们,WWDC 2012 Session 407 的视频中也介绍了它们。
回答by NSZombie
You could try calling [self dismissViewControllerAnimated:YES completion:nil];
from the controller you want to dismiss (whether the controller has been pushed, or shown modally).
您可以尝试[self dismissViewControllerAnimated:YES completion:nil];
从要关闭的控制器调用(无论控制器已被推送,还是以模态显示)。
Here is the related documentation : UIViewController Class Reference
这是相关文档:UIViewController Class Reference
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.
呈现视图控制器负责解除它呈现的视图控制器。如果您在呈现的视图控制器本身上调用此方法,它会自动将消息转发到呈现的视图控制器。
回答by John Ballinger
Just to clarify.
只是为了澄清。
In the class that was pushed. Simply wire up the following and the controller and view will be popped off.
在被推的班级。只需连接以下内容,控制器和视图就会弹出。
[self.navigationController popViewControllerAnimated:YES];
回答by iJames
Create Segue type "Custom" on your stroyboard. This can be from a button. Create a new UIStoryboardSegue class named "popSegue" In the popSegue.m file add the following;
在您的 stroyboard 上创建 Segue 类型“自定义”。这可以来自一个按钮。创建一个名为“popSegue”的新 UIStoryboardSegue 类在 popSegue.m 文件中添加以下内容;
-(void)perform{
UIViewController *sourceViewContreoller = [self sourceViewController];
[sourceViewContreoller.navigationController popViewControllerAnimated:YES];
}
-In the storyboard editor.
- 在故事板编辑器中。
-Select the segue and change the Segue Class to "popSegue"
- 选择转场并将转场类更改为“popSegue”
-Set the Identifier to "popSegue"
- 将标识符设置为“popSegue”
Done!
完毕!
You can use the same "popSegue" class throughout your project.
您可以在整个项目中使用相同的“popSegue”类。
Hope this helps
希望这可以帮助
回答by Bob
I'm using Xcode 5 also and here's how it's done. First, in the view code file that pushed the other, create an IBAction method in the .h file such as this:
我也在使用 Xcode 5,这是它的完成方式。首先,在推送对方的视图代码文件中,在.h文件中创建一个IBAction方法如下:
- (IBAction)exitToHere:(UIStoryboardPopoverSegue *)segue sender:(id)sender;
Then in the .m file add this:
- (IBAction)exitToHere:(UIStoryboardPopoverSegue *)segue sender:(id)sender {
}
You can add any cleanup code you want executed in this method. Next go to your storyboard and select the pushed view. I assume you've got some kind of button on the view that the user taps to signal he's finished. Click on that button, hold down the key and drag to the the green box below the view which is the Exit. Release the mouse button but continue to hold the key. A popup will appear and your method will show in the list. Select that method. Now when the user clicks on the button, the view will pop and you'll be returned to the starting method.
您可以添加要在此方法中执行的任何清理代码。接下来转到您的故事板并选择推送视图。我假设你在视图上有某种按钮,用户点击它来表示他已经完成。单击该按钮,按住该键并拖动到视图下方的绿色框,即退出。释放鼠标按钮但继续按住该键。将出现一个弹出窗口,您的方法将显示在列表中。选择那个方法。现在,当用户单击按钮时,视图将弹出,您将返回到开始方法。