不从子视图控制器弹出到父视图

时间:2013-04-26 11:45:27

标签: iphone ios ipad ios6

我有3个视图控制器“Root”,“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];

}

2 个答案:

答案 0 :(得分:1)

Yo无法弹出到子视图控制器,因为未添加到导航控制器堆栈。 (当你拨打[storyboard instantiateViewControllerWithIdentifier:@"Child"];时)你会创建一个新的Child实例

如果您按下Parent,那么您将子项推送到Parent,如果您从Child [self.navigationController popViewControllerAnimated:YES]打电话,它应该有效。

答案 1 :(得分: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;
            }
        }

希望这可以帮助你.. :)