new、init、initWithFrame、initWithFrame:Style: 的调用顺序

在 控制器中引用了一个继承至UITableView的自定义类。

一. new 初始化

_tableView = [PCAuthorizeTableView new];
- (instancetype)init
{
    NSLog(@"--- init 方法1 --- ");
    self = [super init];
    NSLog(@"--- init 方法2 --- ");
    if (self) {
        NSLog(@"--- init 方法3 --- ");
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    NSLog(@"--- initWithFrame 方法1 --- ");
    self = [super initWithFrame:frame];
    NSLog(@"--- initWithFrame 方法2 --- ");

    if (self) {
        NSLog(@"--- initWithFrame 方法3 --- ");
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    NSLog(@"--- initWithFrame:style: 方法1 --- ");

    self = [super initWithFrame:frame style:style];
    NSLog(@"--- initWithFrame:style: 方法1 --- ");
    if (self) {
        NSLog(@"--- initWithFrame:style: 方法1 --- ");
    }
    
    return self;
}
  1. 先走 init 方法中的 self = [super init];
  2. 再走 - (instancetype)initWithFrame:(CGRect)frame 中的 self = [super initWithFrame:frame];
  3. 来到 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style 中的 self = [super initWithFrame:frame style:style];
  4. 回到 - (instancetype)initWithFrame:(CGRect)frame 中,调用里面的代码返回self。
  5. 回到 init 方法中走完,返回self.
  6. 执行完毕
2022-03-04 10:42:15.903487+0800 PayCredit[9336:1852206] --- init 方法1 --- 
2022-03-04 10:42:15.904262+0800 PayCredit[9336:1852206] --- initWithFrame 方法1 --- 
2022-03-04 10:42:15.904832+0800 PayCredit[9336:1852206] --- initWithFrame:style: 方法1 --- 
2022-03-04 10:42:15.913059+0800 PayCredit[9336:1852206] --- initWithFrame:style: 方法1 --- 
2022-03-04 10:42:15.913835+0800 PayCredit[9336:1852206] --- initWithFrame:style: 方法1 --- 
2022-03-04 10:42:15.914341+0800 PayCredit[9336:1852206] --- initWithFrame 方法2 --- 
2022-03-04 10:42:15.914812+0800 PayCredit[9336:1852206] --- initWithFrame 方法3 --- 
2022-03-04 10:42:15.915292+0800 PayCredit[9336:1852206] --- init 方法2 --- 
2022-03-04 10:42:15.915852+0800 PayCredit[9336:1852206] --- init 方法3 --- 

二、 init 的调用顺序

_tableView = [[PCAuthorizeTableView alloc] init];

 --- init 方法1 --- 
 --- initWithFrame 方法1 --- 
 --- initWithFrame:style: 方法1 --- 
 --- initWithFrame:style: 方法1 --- 
 --- initWithFrame:style: 方法1 --- 
 --- initWithFrame 方法2 --- 
 --- initWithFrame 方法3 --- 
 --- init 方法2 --- 
--- init 方法3 --- 

三、initWithFrame

_tableView = [[PCAuthorizeTableView alloc] initWithFrame:aRect];

 --- initWithFrame 方法1 --- 
 --- initWithFrame:style: 方法1 --- 
 --- initWithFrame:style: 方法1 --- 
 --- initWithFrame:style: 方法1 --- 
 --- initWithFrame 方法2 --- 
 --- initWithFrame 方法3 --- 

四、

_tableView = [[PCAuthorizeTableView alloc] initWithFrame:aRect style:UITableViewStylePlain];

--- initWithFrame:style: 方法1 --- 
--- initWithFrame:style: 方法1 --- 
--- initWithFrame:style: 方法1 --- 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值