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

本文探讨了为什么不能使用UINavigationController的pushViewController方法推送UITabBarController,并从头文件角度分析了两者之间的关系。指出在实际项目中,通常会将UINavigationController嵌入到UITabBarController中,而反之则可能导致混乱。还提到了使用container view controller时的一些最佳实践,如避免添加系统定义的全屏切换的container view controller作为子视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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:欢迎大家一起讨论,多多指教。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值