嵌套的 UIViewController 需要推送到根 UINavigationController

发布于 2024-10-03 11:53:09 字数 1179 浏览 2 评论 0原文

我有一个带有 UINavigationController 的应用程序,其中包含根 UIViewController 子类(PagingViewController),它管理并充当水平分页 UIScrollView 的委托。在这个UIScrollView的每个页面上,都有一个垂直滚动的UITableView,每个UITableView由它自己的UIViewController子类(PageViewController)管理。它看起来像这样...

UINavigationController
  PagingViewController (root VC of navigation controller)
    PageViewController (instance 1)
    PageViewController (instance 2)
    PageViewController (instance 3)

  HowToPushThisViewController (this is what I'd like to push onto the current root, PagingViewController, from one of its sub view controllers)

我想做的是,在 PageViewController (UITableView 委托)的 didSelectRowAtIndexPath: 方法中,将新的 UIViewController 推送到 UINavigationController 上(在 PagingViewController 之上)。

问题是 PageViewController 的 navigationController 属性为零,所以我无法将其推送到那里。我认为这是 nil,因为它不是直接位于 UINavigationController 的堆栈上,而是由 PagingViewController 包含,而 PagingViewController 位于堆栈上。我尝试在将每个 PageViewController 添加到 PagingScrollView 之前设置其 navigationController 属性,但它似乎是只读的(UINavigationController 如何设置自身?)。

我觉得嵌套的 UIViewControllers 是健全的架构(每个都需要管理并充当其视图的委托),并且想要从子控制器推送导航是合理的,所以我不确定访问和访问的最佳实践从嵌套控制器推送。希望有一些我忽略的简单的东西可以访问并推送到根导航控制器。

I have an application with a UINavigationController that contains a root UIViewController subclass (PagingViewController) which manages and acts as the delegate for a horizontally paging UIScrollView. On each of the pages of this UIScrollView, there is a vertically scrolling UITableView, each managed by its own UIViewController subclass (PageViewController). It looks something like this...

UINavigationController
  PagingViewController (root VC of navigation controller)
    PageViewController (instance 1)
    PageViewController (instance 2)
    PageViewController (instance 3)

  HowToPushThisViewController (this is what I'd like to push onto the current root, PagingViewController, from one of its sub view controllers)

What I'd like to do is, in the didSelectRowAtIndexPath: method of the PageViewController (UITableView delegate), push a new UIViewController onto the UINavigationController (on top of the PagingViewController).

The issue is that the navigationController property of the PageViewController is nil, so I can't push it there. I assume this is nil because it is not DIRECTLY on the UINavigationController's stack, but contained by the PagingViewController which is on the stack. I tried setting the navigationController property of each PageViewController before adding it to the PagingScrollView, but it seems to be read only (How does the UINavigationController set itself?).

I feel like the nested UIViewControllers is sound architecture (each one is needed to manage and act as delegate for its views), and it's reasonable to want to push navigation from a sub controller, so I'm not sure the best practice for accessing and pushing from the nested controller. Hopefully there is something simple I'm overlooking to access and push onto the root naviagtion controller.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姐不稀罕 2024-10-10 11:53:09

合乎逻辑的做法是设置页面视图控制器的 parentViewController 属性,但遗憾的是,它也是只读的。 Apple 不鼓励自定义视图控制器层次结构(除了支持的带有选项卡栏、导航和分割视图控制器的层次结构之外)。

在您的情况下,我将在 PageViewController 类上定义一个自定义 parentVC 属性,PagingViewController 在实例化其子级时将其设置为自身。然后,PageViewController可以通过以下方式访问导航控制器

self.parentVC.navigationController

The logical thing to do would be to set the page view controller's parentViewController property but alas, it is read-only too. Apple's does not encourage custom hierarchies of view controllers (besides the supported hierarchies with tab bar, navigation and split view controllers).

In your case, I would define a custom parentVC propertyy on the PageViewController class that the PagingViewController sets to itself when it instantiates its children. The PageViewControllers can then get to the nav controller via

self.parentVC.navigationController
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文