2015-02-24 70 views
0

我用这个代码:任何人都可以解释为什么popToViewController:方法不起作用吗?

MainViewController *mvc = [[MainViewController alloc] init]; 
[self.navigationController popToViewController:mvc animated:YES]; 

,它崩溃,我不知道为什么,或者我错了。错误是: 原因:'试图弹出到不存在的视图控制器。' ,但我的视图控制器存在。

如果有人可以帮助我..

+0

错误是告诉你你的MainViewController实例在navigationController栈中不存在。当您弹出某个视图控制器时,该视图控制器必须存在于导航控制器的堆栈中。 – Steve 2015-02-24 17:40:03

+0

@Steve - 谢谢。现在我明白了! :) – Adela 2015-02-24 17:47:38

回答

0

我建议写在你弹出视图控制器与此类似的方法。

- (void) popCurrentViewController 
{ 
    UIViewController *popDestination = nil; 
    for (UIViewController *viewController in self.navigationController.viewControllers) 
    { 
     if ([viewController isKindOfClass:[MainViewController class]]) 
     { 
      popDestination = viewController; 
     } 
    } 

    if (popDestination) 
    { 
     NSLog(@"%@ Popping...", self); 
     [self.navigationController popToViewController:popDestination animated:true]; 
    } 
    else 
    { 
     // Handle if it can not find the specific view controller. 
    } 
} 
+0

是的,这是一个很好的代码,这就是@Steve所说的。我做了类似的,但谢谢:) – Adela 2015-02-24 17:58:41

相关问题