View的初始化函数

作者: 生如夏花逝如秋叶 | 来源:发表于2016-08-09 14:48 被阅读29次
    ShopView类
    #import "ShopView.h"
    
    @implementation ShopView
    
    - (instancetype)init{
        
        self = [super init];
        
        if (self != nil) {
            
            NSLog(@"%s",__func__);
            
        }
        
        return self;
    }
    
    - (instancetype)initWithFrame:(CGRect)frame{
        
        self = [super initWithFrame:frame];
        
        if (self != nil) {
            
            NSLog(@"%s",__func__);
        }
        
        return self;
    }
    
    @end
    
    
    init 方法内部会调用initWithFrame:方法
    ShopView *shopView = [[ShopView alloc] init];
    

    打印结果

     viewControll生命周期[946:54524] -[ShopView initWithFrame:]
     viewControll生命周期[946:54524] -[ShopView init]
    
    initWithFrame:方法内部不会调用init 方法
    ShopView *shopView = [[ShopView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    
    

    打印结果

     viewControll生命周期[975:57255] -[ShopView initWithFrame:]
    

    相关文章

      网友评论

        本文标题:View的初始化函数

        本文链接:https://www.haomeiwen.com/subject/zeldsttx.html