关于 IOS 开发中遇到的 读取 cell xib 文件时导致死机的问题。

本文详细解析了一个导致iOS APP崩溃的问题及其解决方案。问题出现在使用loadNibNamed加载视图时,因错误地将所有视图连接到File's Owner而非具体的cell对象而导致。文中给出了正确的连接方法,并解释了初始化过程中的误区。

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

下面这句话看上去没有什么问题,但是偏偏在这个地方APP crash了。

cell = [[[NSBundlemainBundle] loadNibNamed:CellIdentifierowner:selfoptions:nil]lastObject];


在google.ca,搜索了下: loadNibNamed crash . 得到了这样的一条关键信息:

http://stackoverflow.com/questions/5479684/crash-on-nsbundle-mainbundle-loadnibnamedowneroptions

这里面的一条关键的回复:

The problem is that you connected the labels to the File's Owner, but when you call loadNibNamed..., you pass nil as the file's owner. You should connect the labels to the cell object in the nib. initWithStyle: will not be called when loading from a nib. Nib loading uses initWithCoder: to initialize the object and calls awakeFromNib after the nib is loaded. 


事实上是由于我之前的误操作:把所有的view 都关联到了 file's ower. 然后通过 file's ower 关联到对应的 IBOutlet 属性。这样做法是有问题的:

当调用了 loadNibNamed ,其中就有个参数, owner:self 这样将直接导致这个 self 与 xib 的 file's owner 不是同一个概念的。所以将直接导致死机。


正确的做法应该是这样:

取消所有xib与file's owner 关联。

直接关联到对应的属性上。


这样就可以了。然后重新编译运行下。问题解决。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值