How to perform Unwind segue programmatically?

2019-01-01 07:41发布

Using storyboard this is very easy. You just drag the action to "Exit". But how should I call it from my code?

标签: ios segue ios6
9条回答
只若初见
2楼-- · 2019-01-01 08:42

I used [self dismissViewControllerAnimated: YES completion: nil]; which will return you to the calling ViewController.

查看更多
妖精总统
3楼-- · 2019-01-01 08:42

Backwards compatible solution that will work for versions prior to ios6, for those interested:

- (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;
        }
    }
}
查看更多
大哥的爱人
4楼-- · 2019-01-01 08:42

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:

[self performSegueWithIdentifier:(NSString*) identifier sender:(id) sender];

inside of the overriden class method viewDidAppear like so:

-(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.

查看更多
登录 后发表回答