How to obtain child view controllers of UIMoreList

2019-04-10 11:09发布

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.

1条回答
叛逆
2楼-- · 2019-04-10 11:50

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];
查看更多
登录 后发表回答