最近做的一个项目界面如下:
UINavigationController
---->UITabBarController
但是看到pushViewController方法中有这样的描述:
"The view controller to push onto the stack. This object cannot be a tab bar controller. "
感到疑惑,为何UINavigationController could be embedded in UITabBarController,反之则不行呢?
转化一下如下:
- UITabBarController -> UINavigationController(YES)
- UINavigationController -> UITabBarController(NO)
我们平时工作中关系1我们一直用,即一般都会为UITabBarController添加几个UINavigationController,那关系1为YES我们可以理解,那这对关系2有什么影响呢,我吃肉也不代表我不吃素啊?先看下两个类的头文件
UITabBarController.h
@interface UIViewController (UITabBarControllerItem) @property(nullable, nonatomic, readonly, strong) UITabBarController *tabBarController; // If the view controller has a tab bar controller as its ancestor, return it. Returns nil otherwise. @end
UINavigationController.h
@interface UIViewController (UINavigationControllerItem) @property(nullable, nonatomic,readonly,strong) UINavigationController *navigationController; // If this view controller has been pushed onto a navigation controller, return it. @end
头文件中可看出任何UIViewController都可以直接获取tabBarController或navigationController。假设以上关系 2 为 YES,如果界面结构如下:
UITabBarController---->UINavigationController---->UITabBarController那么属性tabBarController为哪一个UITabBarController?
*************************************************************************************************************************
关于使用container view controller:
- 自定义的container view controller当不作为根控制器时,最好不要添加系统定义的可整屏切换的container view controller作为子视图,如UITabBarController/UINavigationController,可以添加局部切换的container view controller,如UIPageViewController。
- 自定义container view controller作为根控制器时,其它container view controller最好不要再添加此container view controller作为子视图。
自定义的类似UITabBarController的container view controller只允许添加非container view controller的UIViewController,除非作为root view controller。
自定义的类似UINavigationController的container view controller不允许添加类似UITabBarController的container view controller。