0

I'm trying to make a navigation controller that when rotated to landscape pushes a new view onto the stack and when rotated back to portrait it pops that view. So far I can get the landscape view displayed, but the only when I can get the portrait back by pushing that view controller again onto the stack. I assume that the stack will run out of memory if a user continuously rotates back and forth. When I tried using the popToViewController, nothing happens. Can someone help me get the pop function working?

This method is in my ViewController class:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
       toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        AppDelegate *delegate =
        (AppDelegate *)[[UIApplication sharedApplication] delegate];

        Landscape *landScape=
        [[Landscape alloc] initWithNibName:@"Landscape" bundle:nil];

        [delegate.navController pushViewController:landScape animated:YES];
    }
}

This method is in my Landscape class:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait ||
            toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        AppDelegate *delegate =
        (AppDelegate *)[[UIApplication sharedApplication] delegate];

        ViewController *viewController=
        [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

        [delegate.navController pushViewController:viewController animated:YES];
    }
}
apples
  • 13
  • 1
  • 6

1 Answers1

1

It seems your ViewController should already be on the stack, so you should be able to get it back by calling [delegate.navController popViewControllerAnimated:YES]; instead of [delegate.navController pushViewController:viewController animated:YES];

Sam
  • 1,504
  • 2
  • 13
  • 19
  • Ok. Cool that worked. I placed the [delegate.navController popViewControllerAnimated:YES]; in the Landscape method and it worked. Thanks. – apples Aug 07 '12 at 02:57