dan
dan

Reputation: 729

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.

Upvotes: 3

Views: 7552

Answers (3)

AlexanderN
AlexanderN

Reputation: 2670

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

Upvotes: 5

rpledge
rpledge

Reputation: 61

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

Upvotes: 1

Alladinian
Alladinian

Reputation: 35686

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

Upvotes: 4

Related Questions