UIView的init调用
2016-04-20 21:35
507 查看
UIView的init调用
在这里着重写 UITableViewCell 的init首先讲个背景:
SettingCell继承BaseCell,而BaseCell继承UITableViewCell
1、[[SettingCell alloc] init];
调用顺序:首先调用 SettingCell.m 文件里的 init 函数,此时打印out self~~~~~~~~~~~~SettingCell
再调用 BaseCell.m 文件里的 init 函数,此时打印out self~~~~~~~~~~~~SettingCell
然后会调用底层父类UITableView的 init 函数
其实到这里是一个小的阶段,上面的阶段一调用时各层的 init 函数下面继续
之后会调用 SettingCell.m 文件里的 initWithFrame 函数,此时打印out self~~~~~~~~~~~~SettingCell
再调用 BaseCell.m 文件里的 initWithFrame 函数,此时打印out self~~~~~~~~~~~~SettingCell
然后会调用底层父类UITableView的 initWithFrame 函数
到这里是一个另外的一个小的阶段,这个阶段二调用的是各层的 initWithFrame 函数,下面继续
再调用 SettingCell.m 文件里的 initWithStyle函数,此时打印out self~~~~~~~~~~~~SettingCell
调用 BaseCell.m 文件里的 initWithStyle 函数,此时打印out self~~~~~~~~~~~~SettingCell
注意:这里到底是否调用了UITableView的 initWithStyle 函数了???(按理说应该要调用,但是没有看到线程)
到这里已经到达BaseCell.m 文件里的if(self)函数里面了,打印的是 in self~~~~~~~~~~~~SettingCell
然后就开始出各层的函数了 按照上面进入函数的顺序(类似于栈,先进后出),逆序出函数
out 和 in 打印的个数一一对应,打印了6个
out 个数:和in 个数相同
SettingCell 的 init 函数
BaseCell 的 init 函数
SettingCell 的 initWithFrame 函数
BaseCell 的 initWithFrame 函数
SettingCell 的 initWithStyle 函数
BaseCell 的 initWithStyle 函数
2、[[SettingCell alloc] initWithFrame:]
在initWithFrame的初始化过程中,打印了4个out,道理很简单,第一个阶段的init舍去了3、[[SettingCell alloc] initWithStyle:]
在initWithStyle的初始化过程中,打印了2个out,舍去了第一个阶段的init和第二个阶段的initWithFrame4、[[NSBundle mainBundle] loadNibNamed:@”SettingCell”…]
在用xib初始化的过程中,打印了2个out,直接进入了initWithCoder函数里,一层层的往里进入,最后出来5、总结
在init的过程中,会一层层的往里进入(其他的View是不是这样可以自行验证),然后再一层层出来,像栈那样先入后出,最后调用完毕关于每次打印都是SettingCell问题:
这个不会因为进入到哪个类的函数中,调用的self.class就变成了这个类,而是最开始调用它的主体是谁,那么以后调用 父类 和 子类 的实体都还是这个主体对象,所以打印的一直是SettingCell
6、代码(O(∩_∩)O哈哈~)
只写了一部分,自己创建两个类,一个父类(BaseCell)和一个子类(SettingCell),然后在.m文件里copy如下代码,就可以验证啦 == (*^__^*) 嘻嘻……- (instancetype)initWithCoder:(NSCoder *)coder { NSLog(@"out self~~~~~~~~~~~~%@",self.class); self = [super initWithCoder:coder]; if (self) { self.selectedBackgroundView = [[UIView alloc] init]; NSLog(@"in self~~~~~~~~~~~~%@",self.class); } return self; } - (instancetype)initWithFrame:(CGRect)frame { NSLog(@"out self~~~~~~~~~~~~%@",self.class); self = [super initWithFrame:frame]; if (self) { self.selectedBackgroundView = [[UIView alloc] init]; NSLog(@"in self~~~~~~~~~~~~%@",self.class); } return self; } - (instancetype)init { NSLog(@"out self~~~~~~~~~~~~%@",self.class); self = [super init]; if (self) { self.selectedBackgroundView = [[UIView alloc] init]; NSLog(@"in self~~~~~~~~~~~~%@",self.class); } return self; } -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { NSLog(@"out self~~~~~~~~~~~~%@",self.class); self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.selectedBackgroundView = [[UIView alloc] init]; NSLog(@"in self~~~~~~~~~~~~%@",self.class); } return self; }
相关文章推荐
- iOS应用开发中UIView添加边框颜色及设置圆角边框的方法
- CALayer与UIView的关系
- UIView的bounds和frame区别
- 自定义uitableviewcell
- uiview
- uiview生命周期
- UIView翻译 (参考)
- iOS 转变UIView到UIImage
- UIView动画
- Swift-UITableViewCell:设置表格的accessoryType属性
- initWithFrame 和 initWithCoder
- [CS193P] 第六堂課摘要及心得筆記
- UI第一天
- ios关于tableView的重用
- tableViewCell的不同高度创建
- UITableViewCell高度自适应
- 棋盘
- ios实现button变换颜色并可以放大、缩小、旋转
- uiview contentViewmode
- iphone开发小记 基础知识2