OC- navigationController 为nil 的情况

在iOS开发中,遇到一个子控制器在viewDidLoad时获取navigationController为nil的情况。问题源于addChildViewController、addSubview和设置frame的调用顺序。addChildViewController用于创建父子控制器关系,必须在添加子控制器的view之前调用,以使子控制器能访问到父控制器的navigationController。如果在viewDidLoad之后调用,由于加载时机问题,可能导致无法获取到navigationController。调整顺序或在正确位置进行操作可以避免这个问题。
摘要由CSDN通过智能技术生成

前言

在工作中遇到这样一个问题 一个容器有两个childController 在子Controller 中的viewDidLoad中获取self.navigationController 的时候发现为nil 不理解 addChildViewController:  addSubview: 以及设置view的frame方法的调用顺序影响了

addChildViewController: 在iOS5.0之后开始使用 一个controller 对应一个view 降低耦合度

当然一系列的调用顺序大家也要注意..别的博客中也给了大体的顺序.

在developer 中可以看到 

Discussion

This method creates a parent-child relationship between the current view controller and the object in the childController parameter. This relationship is necessary when embedding the child view controller’s view into the current view controller’s content. If the new child view controller is already the child of a container view controller, it is removed from that container before being added.

This method is only intended to be called by an implementation of a custom container view con

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值