popToViewController:'NSInternalInconsistencyException',原因:'试图弹出到不存在的视图控制器。'

时间:2010-03-18 10:29:49

标签: ios iphone uiviewcontroller uinavigationcontroller

我的第二个观点是BController * bview。 现在在这个视图中有1个后退按钮

点击后退按钮

-(IBAction)done:(id)sender
{
AController *aview= [[AController alloc] initWithNibName:@"AController" bundle:[NSBundle mainBundle]];

    NSArray *array = [self.navigationController popToViewController: aview animated:YES];

}

AController * aview只不过是我的第一个视图,或者你可以说第一个视图

但是点击后退按钮我得到了异常

**由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'尝试弹出到不存在的视图控制器。 2010-03-18 15:53:05.948 IChitMe [5072:207] Stack :(     820145437,     837578260,     819694387,     814928571,     862794500,     862794216,     54911,     819902607,     861951876,     862404412,     819902607,     861951876,     861951724,     861951668,     861950732,     861953932,     861948160,     861945748,     861927312,     861925524,     858687888,     819893547,     819891231,     858682228,     861592624,     861585968,     10069,     9964 ) 抛出'NSException'实例后调用终止

2 个答案:

答案 0 :(得分:24)

使用

[self.navigationController popViewControllerAnimated:YES];

OR

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

答案 1 :(得分:6)

navigationController管理UIViewControllers的堆栈。这就像一叠牌。当你调用其中一个pop方法时:

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
- (UIViewController *)popViewControllerAnimated:(BOOL)animated

从顶部删除一些控制器,并显示相应的控制器:

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated  

弹出到第一个(即堆栈中最低的)控制器,它被称为“root”。

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated 

弹出到指定的viewController,注意它应该已经在堆栈中!

- (UIViewController *)popViewControllerAnimated:(BOOL)animated  

弹出到前一个控制器(低于当前值)。

因此,如果你想使用pop ...导航控制器的方法显示你的AController * aview它应该已经在navigationController堆栈中 - 这是navigationController的 viewControllers 属性:

@property(nonatomic, copy) NSArray *viewControllers

例如:

aController = [[AController alloc] initWithNibName:@"AController" bundle:[NSBundle mainBundle]];
bController = [[BController alloc] initWithNibName:@"BController" bundle:[NSBundle mainBundle]];
navigationController = [[UINavigationController alloc] initWithRootViewController:aController]; 
[navigationController pushViewController:bController];

现在显示bController,您可以致电:

[navigationController popToViewController:aController animated:YES];