-1

我有 3 个视图控制器“根”、“父”和“子”。现在我正在从 Parent 的方法中推入 Child。现在,当我想通过以下代码从子视图弹出到父级时:

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

当我编写以下代码时,它会弹出到 Root。:

[self.navigationController popToRootViewControllerAnimated:YES];

但我想准确地弹出到父视图。我该怎么做?

提前致谢。

从类 Parent 推送示例:

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

来自 Child 的流行示例:

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

}
4

2 回答 2

1

Yo 不能弹出到子视图控制器,因为没有添加到导航控制器堆栈中。(调用时创建一个新的 Child 实例[storyboard instantiateViewControllerWithIdentifier:@"Child"];

如果您推送父项,然后将子项从父项中推送,并且如果您从子项调用[self.navigationController popViewControllerAnimated:YES]它应该可以工作。

于 2013-04-26T11:50:12.633 回答
0

要弹出视图控制器,您可以使用以下代码...

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

希望这对你有帮助..:)

于 2013-04-26T12:24:55.023 回答