Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 700
dan Avatar asked May 05 '26 06:05

dan


2 Answers

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

like image 103
AlexanderN Avatar answered May 07 '26 19:05

AlexanderN


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

like image 32
Alladinian Avatar answered May 07 '26 20:05

Alladinian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!