不断推动视图控制器

时间:2012-08-06 22:10:19

标签: iphone objective-c uiviewcontroller ios5.1

我正在尝试制作一个导航控制器,当旋转到横向时将新视图推到堆栈上,当旋转回到肖像时,它会弹出该视图。到目前为止,我可以显示横向视图,但只有当我可以通过将视图控制器再次推入堆栈时才能恢复纵向。我假设如果用户连续前后旋转,堆栈将耗尽内存。当我尝试使用popToViewController时,没有任何反应。有人可以帮助我让pop功能正常工作吗?

此方法在我的ViewController类中:

-(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];
    }
}

此方法在我的Landscape类中:

-(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];
    }
}

1 个答案:

答案 0 :(得分:1)

您的ViewController似乎已经在堆栈中,因此您应该可以通过调用[delegate.navController popViewControllerAnimated:YES];而不是[delegate.navController pushViewController:viewController animated:YES];

来取回它