Navigation controller note

programmatically executing segues in iOS is an interesting problem.  this started because I wanted to execute a segue during a change in orientation. to perform the original segue, I added the following method to the view controller that is on the screen:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
            duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
        [self performSegueWithIdentifier:@"rotateme" sender:self];
    }
}

no big problem. the difficulty comes in trying to get back to the original screen. you could set up another named segue and do the following:

[self performSegueWithIdentifier:@"namedSegue" sender:self];

which seems easy, but navigation controllers are a stack. if you segue back, you add to the stack, what you need to do is go back or ‘unwind’ one step, or pop the view off the stack.:

[self.navigationController popViewControllerAnimated:YES];

if for some reason you have actually moved more than one screen and want to skip right back to the original (or root) view, you can just use the following:

[self.navigationController popToRootViewControllerAnimated:TRUE];

Leave a Reply

Your email address will not be published. Required fields are marked *