如何以编程方式执行Unwind segue?

本文介绍如何在iOS应用中以编程方式执行UnwindSegue。通过创建手动Segue,并在ViewController的viewDidAppear方法中调用performSegueWithIdentifier,可以实现从当前视图控制器返回到前一个视图控制器的功能。

本文翻译自: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楼

  1. Create a manual segue ( ctrl -drag from File's Owner to Exit), 创建一个手动序列(从文件所有者的ctrl -drag拖动到Exit),
  2. Choose it in the Left Controller Menu below green EXIT button. 在绿色EXIT按钮下方的左侧控制器菜单中选择它。

在绿色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方法(如viewDidLoadviewWillAppear)中 ,它将被忽略。


#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;
        }
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值