简体   繁体   中英

xcode unwind segue programmatically

How can we do a push xcode unwind segue programmatically?

I know the simple way is to use the storyboard and pull the button to the 'exit', but what I intend to do is to have some codes run first before the view is exited.

To go back to the rootViewController use:

[self.navigationController popToRootViewControllerAnimated:(BOOL)]

Or to go to a specific controller:

[self.navigationController popToViewController:
    [[self.navigationController viewControllers]
    objectAtIndex:THEINDEXOFTHEVIEWCONTROLLERTOUNWINDTO]
    animated:YES];

For a little more detail, see my answer in this post: Push to root View controller in UIStoryboard

If you just want to execute some code before the exit (although keep it simple otherwise you'll get a UI lock until you return something), you could override canPerformUnwindSegueAction:fromViewController:withSender: on your destination controller.

Something like this perhaps:

- (BOOL)canPerformUnwindSegueAction:(SEL)action 
                 fromViewController:(UIViewController *)fromViewController 
                         withSender:(id)sender
{
    // Some operation...

    return YES; // Or NO if something went wrong and you want to abort
}

Otherwise, you could actually create the segue programmatically and manage animation/unwind logic by overriding segueForUnwindingToViewController:fromViewController:identifier: .

You can find the complete documentation here

You can have a segue occur in code by calling this method in UIViewController

- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender

You'll still create the segue in your storyboard between the 2 views, just make sure you name the segue and use the exact name in the identifier parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM