2014-05-15 70 views
1

我正在为一个项目设计一个自定义UIViewContainerController。我在此容器中保存了一个contentView以管理我的Childviewcontroller's视图。当我单独添加childviewcontroller时,它工作正常。但是一些childviewcontrollers必须使用“导航控制器”&进行初始化,这是我遇到实现问题的地方。将带有NavigationController的ChildViewController添加到ContainerController

我用平时initWithRootViewController方法上“navigationcontroller”给init(初始化)我的“childvc” &那我怎么与导航栏一起加入这个我contentView

这是我使用的代码“childvc”没有“导航控制器”&它工作正常。

// in my containerview controller's add childview method. 
ChildViewController1 *vc = [ChildViewController1 new]; 

[self addChildViewController:vc]; // self = container vc 
vc.view.frame = self.contentView.bounds; 
[self.contentView addSubview:vc.view]; // contentView is the space i've kept to add childvcs 
[vc didMoveToParentViewController:self]; 

现在,当我尝试使用“childvc”与“navigationcontroller”初始化(因为有这个“childvc”甲流),我得到的错误&什么,我需要知道的是我怎么将它添加到我的contentView以及导航栏。 (就像在tabbarcontroller中)。

这是我使用初始化“childvc”与“导航控制器”的代码:

ChildViewController1 *vc = [ChildViewController1 new]; 
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; 

我做了简单的工程“Here”公共仓库。

我已阅读标签栏/导航控制器的文档&在苹果文档中创建自定义容器视图控制器,但似乎缺少重要的东西。 链接是“Here”。

回答

1

从看着你的公开回购注释代码,你正在尝试做的是这样的:

container VC 
    +-- navigation VC 
    |  +-- child VC 
    +-- child VC 

这是不对的,孩子VC只能一次在视图控制器层次出现。您的层次结构应如下所示:

container VC 
    +-- navigation VC 
     +-- child VC 

下面是如何设置此代码的代码的粗略草图。注意导航控制器(及其视图)如何完全取代ChildViewController1

// Setup the view controller hierarchy - place this inside 
// your container VC's initializer 
ChildViewController1* vc = [ChildViewController1 new]; 
// With this statement, vc becomes the child of the navigation controller 
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:vc]; 
[self.childViewControllers addObject:nav]; 
[nav didMoveToParentViewController:self]; 

// Setup the view hierarchy - place this inside your 
// container VC's loadView override 
nav.view.frame = self.contentView.bounds; 
[self.contentView addSubview:nav.view]; 

正如在评论中提到的,我建议您将视图控制器层次结构的设置与视图层次结构的设置分开。

  • 视图控制器设置的典型场所是初始化期间。您可以看到UINavigationController如何通过其initWithRootViewController:初始值设定项执行此操作。
  • 视图层次结构设置的规范位置在视图控制器的覆盖范围loadView中。
相关问题