本文翻译自:How to perform Unwind segue programmatically?
Using storyboard this is very easy. 使用情节提要非常简单。 You just drag the action to "Exit". 您只需将动作拖到“退出”即可。 But how should I call it from my code? 但是如何从代码中调用它呢?
#1楼
参考:https://stackoom.com/question/qUGs/如何以编程方式执行Unwind-segue
#2楼
- Create a manual segue ( ctrl -drag from File's Owner to Exit), 创建一个手动序列(从文件所有者的ctrl -drag拖动到Exit),
- Choose it in the Left Controller Menu below green EXIT button. 在绿色EXIT按钮下方的左侧控制器菜单中选择它。

Insert Name of Segue to unwind. 插入“名称”以展开。
Then, - (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender. 然后, - (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender. with your segue identify. 与您的segue标识。
#3楼
I used [self dismissViewControllerAnimated: YES completion: nil]; 我用了[self dismissViewControllerAnimated: YES completion: nil]; which will return you to the calling ViewController . 这将使您返回到调用ViewController 。
#4楼
FYI: In order for @Vadim's answer to work with a manual unwind seque action called from within a View Controller you must place the command: 仅供参考:为了使@Vadim的答案与在View Controller中调用的手动展开固定动作配合使用,必须放置以下命令:
[self performSegueWithIdentifier:(NSString*) identifier sender:(id) sender];
inside of the overriden class method viewDidAppear like so: 在覆盖的类方法viewDidAppear中,如下所示:
-(void) viewDidAppear:(BOOL) animated
{
[super viewDidAppear: animated];
[self performSegueWithIdentifier:@"SomeSegueIdentifier" sender:self];
}
If you put it in other ViewController methods like viewDidLoad or viewWillAppear it will be ignored. 如果将其放在其他ViewController方法(如viewDidLoad或viewWillAppear)中 ,它将被忽略。
#5楼
Quoting text from Apple's Technical Note on Unwind Segue: To add an unwind segue that will only be triggered programmatically, control+drag from the scene's view controller icon to its exit icon, then select an unwind action for the new segue from the popup menu. 引用Apple的Unwind Segue技术注释中的文本:要添加只能以编程方式触发的Unwind Segue,请按住Control键并将其从场景的视图控制器图标拖到其退出图标,然后从弹出菜单中为新的Segue选择一个Unwind操作。
Link to Technical Note 链接到技术说明
#6楼
Backwards compatible solution that will work for versions prior to ios6, for those interested: 向后兼容的解决方案将对那些感兴趣的人适用于ios6之前的版本:
- (void)unwindToViewControllerOfClass:(Class)vcClass animated:(BOOL)animated {
for (int i=self.navigationController.viewControllers.count - 1; i >= 0; i--) {
UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:i];
if ([vc isKindOfClass:vcClass]) {
[self.navigationController popToViewController:vc animated:animated];
return;
}
}
}
本文介绍如何在iOS应用中以编程方式执行UnwindSegue。通过创建手动Segue,并在ViewController的viewDidAppear方法中调用performSegueWithIdentifier,可以实现从当前视图控制器返回到前一个视图控制器的功能。
110

被折叠的 条评论
为什么被折叠?



