UINavigationController方法pushViewController:参数不能传递UITabBarController原因猜想

来源:互联网 发布:淘宝缺货赔付规则 编辑:程序博客网 时间:2024/05/29 11:19

最近做的一个项目界面如下:

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,反之则不行呢?

转化一下如下:

  1. UITabBarController -> UINavigationController(YES)
  2. UINavigationController -> UITabBarController(NO)
同样是苹果官方提供的container view controller,为什么会有这个区别呢?

*************************************************************************************************************************

再次声明以下原因仅为个人猜想:

    鉴于使用习惯和API设计考虑
80%-90%的app是以UITabBarController为根控制器,UINavigationController作为其中的tab,故关系1YES,api设计上我们看下头文件,tabBarController/navigationController通过扩展为UIViewContoller添加属性,如果关系2 YES,以下头文件可看出,会导致tabBarController不知道指向哪一个。
  1. 我们平时工作中关系1我们一直用,即一般都会为UITabBarController添加几个UINavigationController,那关系1YES我们可以理解,那这对关系2有什么影响呢,我吃肉也不代表我不吃素啊?
  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作为子视图。

  1. 自定义的类似UITabBarController的container view controller只允许添加非container view controller的UIViewController,除非作为root view controller。
  2. 自定义的类似UINavigationController的container view controller不允许添加类似UITabBarController的container view controller。
PS:欢迎大家一起讨论,多多指教。


0 0
原创粉丝点击