如何获取UIMoreListController(私有框架类)的子视图控制器?

发布于 2024-12-10 18:15:12 字数 1218 浏览 0 评论 0原文

我很好奇是否有一个合法的 API 可以用来获取 UIMoreListController 的子视图控制器? (UIMoreListController 是 UIMoreNavigationController 视图堆栈顶部的子级。UIMoreNavigationController 是 UITabBarControllermoreNavigationController 属性所指向的对象。)

换句话说,如果我有一个 UITabBarController 并在其上设置了一个由六个视图控制器组成的数组,视图层次结构实际上看起来像这样(它实际上是视图层次结构,不是视图控制器,而是使用这些标识符更有意义):

+ UITabBarController <-- has five tabs
|--- view controller     <-- these are my own view controllers
|--- view controller
|--- view controller
|--- view controller
|--+ UIMoreNavigationController <-- root view controller of fifth tab
   |--+ UIMoreListController    <-- table-based view shown on fifth tab
      |--- view controller      <-- these are my own view controllers
      |--- view controller

一种方法是简单地从 UITabBarController 的 viewControllers 属性获取视图控制器,检查是否超过 5 个,然后将索引 5 中的所有视图控制器返回到N - 1 如果超过 5 个。(如果少于 5 个,则没有视图控制器将成为 UIMoreNavigationController 的子级。)

但是,我想避免对任何假设进行硬编码在 moreNavigationController 中列出剩余控制器之前,UITabBarController 将显示的视图控制器的数量。苹果将​​来可能会改变这个数字。但我在 UITabBarController 或 UINavigationController 上找不到任何用于访问这些子项的 API,并且 UIMoreNavigationController 不是公共类,因此我不能依赖该类上公开的任何方法。

I'm curious if there is a legitimate API with which to obtain the view controllers that are children of a UIMoreListController? (The UIMoreListController is the child at the top of the UIMoreNavigationController's view stack. The UIMoreNavigationController is the object pointed to by the moreNavigationController property of a UITabBarController.)

In other words, if I have a UITabBarController and I set an array of six view controllers on it, the view hierarchy will actually look like this (it's actually a hierarchy of views, not view controllers, but using these identifiers make more sense):

+ UITabBarController <-- has five tabs
|--- view controller     <-- these are my own view controllers
|--- view controller
|--- view controller
|--- view controller
|--+ UIMoreNavigationController <-- root view controller of fifth tab
   |--+ UIMoreListController    <-- table-based view shown on fifth tab
      |--- view controller      <-- these are my own view controllers
      |--- view controller

One way to do this is to simply get the view controllers from the UITabBarController's viewControllers property, check to see if there are more than 5, and return all the view controllers from index 5 to N - 1 if there are more than 5. (If there are less than 5, then no view controllers will be children of the UIMoreNavigationController.)

However, I'd like to avoid hardcoding any assumptions about the number of view controllers that a UITabBarController will display before listing the remaining controllers in the moreNavigationController. Apple could change that number in the future. But I can't find any API on either UITabBarController or on UINavigationController with which to access these children, and UIMoreNavigationController is not a public class so I can't rely on any methods exposed on that class.

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

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

发布评论

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

评论(1

心如狂蝶 2024-12-17 18:15:12

我还没有找到真正的 API 来完成这个任务。但是,无需基于检查 UIUserInterfaceIdiom 并假设最多 5 或 8 个选项卡进行假设即可完成此操作。它有点丑陋,也很简单:

  1. 获取选项卡栏中的项目数。 UITabBarController.tabBar.items 将包含 moreNavigationController 的选项卡栏项目(如果有这样的选项卡栏项目)。
  2. 计算 UITabBarController 中视图控制器的数量。如果 UITabBarController 中的视图控制器多于选项卡栏项目,则最后一个选项卡栏项目代表 moreNavigationController
  3. 因此,将选项卡栏项目数减一(以考虑 moreNavigationController),这就是 moreNavigationController 中第一个子项的索引UITabBarController.viewControllers。

或者,在代码中:

NSUInteger tabCount = [tabBarController.tabBar.items count];
NSUInteger vcCount = [tabBarController.viewControllers count];
NSUInteger idx = (vcCount > tabCount) ? tabCount - 1 : 0;
NSIndexSet *is = [NSIndexSet indexSetWithRange:NSMakeRange(idx, vcCount - idx)];
NSArray *moreControllers = [tabBarController.viewControllers objectsAtIndexes:is];

I still haven't found a real API for accomplishing this. However, it can be done without making assumptions based on checking the UIUserInterfaceIdiom and assuming a max of 5 or 8 tabs. It's kind of ugly, kind of simple:

  1. Obtain the number of items in the tab bar. UITabBarController.tabBar.items will include the tab bar item for the moreNavigationController, if there is such a tab bar item.
  2. Count the number of view controllers in the UITabBarController. If there are more view controllers in the UITabBarController than there are tab bar items, then the last tab bar item represents the moreNavigationController.
  3. Therefore, decrement the count of the number of tab bar items by one (to account for the moreNavigationController) and that's the index of the first child of the moreNavigationController within UITabBarController.viewControllers.

Or, in code:

NSUInteger tabCount = [tabBarController.tabBar.items count];
NSUInteger vcCount = [tabBarController.viewControllers count];
NSUInteger idx = (vcCount > tabCount) ? tabCount - 1 : 0;
NSIndexSet *is = [NSIndexSet indexSetWithRange:NSMakeRange(idx, vcCount - idx)];
NSArray *moreControllers = [tabBarController.viewControllers objectsAtIndexes:is];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文