不会弹出到子视图控制器的父视图

问题描述:

我有3个视图控制器“根”,“父”&“孩子”。现在我正从父母的方法推进孩子。现在,当我想通过下面的代码从子视图弹出父:不会弹出到子视图控制器的父视图

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil]; 
Parent *svc = [storyboard instantiateViewControllerWithIdentifier:@"Parent"]; 

[self.navigationController popToViewController:svc animated:YES]; 

这显示了错误:

'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.' 

当我写了下面的代码代替,它弹出一个空白屏幕! :

[self.navigationController popViewControllerAnimated:YES]; 

而当我写下面的代码,它弹出到根。 :

[self.navigationController popToRootViewControllerAnimated:YES]; 

但我想正好弹出到父视图。我该怎么做?

在此先感谢。从类父

推例如:从儿童

-(void)Custom{ 

if([info isEqualToString:@"message"]){ 

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil]; 

Child *cd = [storyboard instantiateViewControllerWithIdentifier:@"Child"]; 

[self.navigationController pushViewController:cd animated:YES]; 
    } 
} 

流行例如:

-(void)viewDidLoad{ 

[super viewDidLoad]; 

[self sendMessage] 

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil]; 
Parent *svc = [storyboard instantiateViewControllerWithIdentifier:@"Parent"]; 

[self.navigationController popToViewController:svc animated:YES]; 

} 

哟无法弹出到儿童视图控制器因为没有添加到导航控制器堆栈。 (当你打电话给[storyboard instantiateViewControllerWithIdentifier:@"Child"];时,你创建了一个新的子实例)

如果你推父,之后你把子窗体推到父窗口,如果你从子窗口调用[self.navigationController popViewControllerAnimated:YES]它应该工作。

+0

对不起,我在这个问题写了一些错误的榜样。我编辑过。请立即检查。 – Leo 2013-04-26 12:02:29

+0

我的回答是有效的,但是你可以在你推送和弹出视图控制器的地方添加代码吗? – danypata 2013-04-26 12:05:41

+0

我在问题部分添加了推送和弹出代码。请立即检查。 – Leo 2013-04-26 12:19:14

对于弹出视图控制器,可以使用下面的代码...

NSArray *viewContrlls=[[self navigationController] viewControllers]; 
     for(int i=0;i<[ viewContrlls count];i++){ 
      id obj=[viewContrlls objectAtIndex:i]; 
      if([obj isKindOfClass:[<yourViewController> class]]){ 

       [[self navigationController] popToViewController:obj animated:YES]; 
       return; 
      } 
     } 

希望这有助于你.. :)