UINavigationcontroller Push和Pop

时间:2011-07-27 06:56:36

标签: iphone controller uinavigation

单击自定义后退按钮后,我必须从当前视图控制器弹出到HomeScreenViewController,后者在主窗口上添加为灯箱。我使用了以下代码:

HomeScreenViewController *homeController = [[HomeScreenViewController alloc] 
initWithNibName:@"HomeScreenViewController" bundle:nil];
    [self.navigationController popToViewController:homeController animated:YES];
    [homeController release];

我遇到异常崩溃:Tried to pop to a view controller that doesn't exist.

如何实施?实施它需要做哪些更改?

3 个答案:

答案 0 :(得分:8)

显然,您正在创建导航堆栈中不存在的HomeScreenViewController的新实例。您必须获取现有实例并将其用作popToViewController:animated:方法的参数。您可以通过viewControllers数组中的UINavigationController数组获取视图控制器来完成此操作。它们按顺序编制索引,因此如果视图控制器位于索引1,则使用

获取视图控制器
UIViewController * viewController = [self.navigationController.viewControllers objectAtIndex:1];
[self.navigationController popToViewController:viewController animated:YES];

如果您想返回根视图控制器,请改用popToRootViewControllerAnimated:

答案 1 :(得分:1)

试试这位朋友

[self.navigationController popToRootViewControllerAnimated:YES/NO];

答案 2 :(得分:0)

另外,请不要忘记AppDelegate(.m)中的此代码:

navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

self.navigationController = navigationController;
self.window.rootViewController = self.navigationController;

[self.window makeKeyAndVisible];

和AppDelegate(.h)

@property (strong, nonatomic) UINavigationController *navigationController;

当您在应用中自定义大量内容时,有时会出现问题。