gpt4 book ai didi

ios - 将带有 NavigationController 的 ChildViewController 添加到 ContainerController

转载 作者:行者123 更新时间:2023-11-29 12:46:00 24 4
gpt4 key购买 nike

我正在为项目设计自定义 UIViewContainerController。我在这个容器中保留了一个 contentView 来管理我的 Childviewcontroller 的 View 。当我单独添加一个 childviewcontroller 时,它工作正常。但是一些 childviewcontrollers 必须使用“navigationcontroller”进行初始化,这就是我在实现时遇到问题的地方。

我在“navigationcontroller”上使用常用的 initWithRootViewController 方法来初始化(初始化)我的“childvc”,然后如何将它与导航栏一起添加到我的 contentView?

这是我在没有“navigationcontroller”的情况下用于“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];

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

这是我用“nav controller”初始化“childvc”的代码:

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

我用简单的项目“Here”创建了公共(public)存储库。

我在苹果文档中阅读了标签栏/导航 Controller 和创建自定义容器 View Controller 的文档,但似乎缺少一些重要的东西。链接是“Here”。

最佳答案

通过查看您的公共(public)存储库中的注释代码,您正在尝试做的是:

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

这是错误的,子 VC 在 View Controller 层次结构中只能出现一次。您的层次结构应如下所示:

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

下面是设置代码的粗略草图。请注意导航 Controller (及其 View )如何完全替换 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];

如评论中所述,我建议您将 View Controller 层次结构的设置与 View 层次结构的设置分开。

  • View Controller 设置的典型位置是在初始化期间。您可以看到 UINavigationController 如何使用其 initWithRootViewController: 初始值设定项执行此操作。
  • View 层次结构设置的规范位置在 View Controller 的 loadView 覆盖中。

关于ios - 将带有 NavigationController 的 ChildViewController 添加到 ContainerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23686430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com