建立控制項系統自動調用方法的小小研究,控制項系統自動

來源:互聯網
上載者:User

建立控制項系統自動調用方法的小小研究,控制項系統自動

    閑來蛋疼,想看看每當我們以各種方式建立一個控制項的時候,系統底層到底幫我們做了什麼事兒!相信各位看官在開發中經常也會把系統自動調用的一些方法給攔截下來,再在這些方法中添加自己想實現的某個功能的代碼。下面小小研究了某些控制項建立時候系統自動協助我們調用的某些方法。
    我們建立控制項的方法有三種
     1.純程式碼
     2.storyboard
     3.xib
    
    建立控制項的過程中系統會自動調用底層的一些方法,方法常見的大概如下
     init
     initWithFrame
     initWithStyle
     initWithCoder
     awakeFromNib
    
    為了研究控制項建立的時候,我們自訂一個testView類,其繼承於UIView,擷取UIView中所有的方法和屬性,將其中init、initWithFrame、initWithCoder、awakeFromNib攔截並且加入列印本方法名的代碼,代碼如下

 1      #import "testView.h" 2  3      @implementation testView 4  5      -(instancetype)init{ 6      if (self = [super init]) { 7      NSLog(@"%s",__func__); 8      } 9      return self;10      }11 12      -(instancetype)initWithCoder:(NSCoder *)aDecoder{13      if (self = [super initWithCoder:aDecoder]) {14      NSLog(@"%s",__func__);15      }16      return self;17      }18 19      -(instancetype)initWithFrame:(CGRect)frame{20      if (self = [super initWithFrame:frame]) {21      NSLog(@"%s",__func__);22      }23      return self;24      }25 26      -(void)awakeFromNib{27      NSLog(@"%s",__func__);28      }29 30      @end

一、在控制器中通過純程式碼的方式建立自訂的testView觀察其底層調用的方法
    代碼:
     testView * test = [[testView alloc] init];
    
    控制台列印輸出:
     2015-12-31 13:49:42.514測試代碼[901:157862] -[testView initWithFrame:]
     2015-12-31 13:49:42.514測試代碼[901:157862] -[testView init]
    
    結論一:通過純程式碼的方式建立空間會自動調用initWithFrame方法
    
    二、通過xib的方式建立
    
    代碼:
         testView * test = [[[NSBundle mainBundle] loadNibNamed:@"test" owner:nil options:nil] firstObject];
    控制台列印輸出:
     2015-12-31 13:59:00.724測試代碼[992:192250] -[testView initWithCoder:]
     2015-12-31 13:59:00.724測試代碼[992:192250] -[testView awakeFromNib]
    
    結論二:通過xib的方式建立控制項,系統會自動調用initWithCoder和awakeFromNib方法
    
    三、建立UITableViewCell,多了一種initWithStyle的方法,再看看建立UITableViewCell時系統會幫我們做什麼事兒叻?
    還是一樣先自訂一個testCell類繼承於UITableViewCell
     testCell代碼:

 1     #import "testCell.h" 2  3      @implementation testCell 4  5      -(instancetype)init{ 6      if (self = [super init]) { 7      NSLog(@"%s",__func__); 8      } 9      return self;10      }11 12      -(instancetype)initWithCoder:(NSCoder *)aDecoder{13      if (self = [super initWithCoder:aDecoder]) {14      NSLog(@"%s",__func__);15      }16      return self;17      }18 19      -(instancetype)initWithFrame:(CGRect)frame{20      if (self = [super initWithFrame:frame]) {21      NSLog(@"%s",__func__);22      }23      return self;24      }25 26      -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{27      if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {28      NSLog(@"%s",__func__);29      }30      return self;31      }32 33      -(void)awakeFromNib{34      NSLog(@"%s",__func__);35      }36 37      - (void)setSelected:(BOOL)selected animated:(BOOL)animated {38      [super setSelected:selected animated:animated];39 40      // Configure the view for the selected state41      }42 43      @end

一個一個來研究,先看init建立
    代碼:
     testCell * cell = [[testCell alloc] init];
    
    控制台:
     2015-12-31 14:11:31.180測試代碼[1030:236258] -[testCell initWithStyle:reuseIdentifier:]
     2015-12-31 14:11:31.181測試代碼[1030:236258] -[testCell initWithFrame:]
     2015-12-31 14:11:31.181測試代碼[1030:236258] -[testCell init]
    
    結論三:建立一個tableViewCell的時候系統底層會自動調用initWithStyle:reuseIdentifier方法和initWithFrame方法,並且是先調用initWithStyle方法,再調用initWithFrame,從正常思考的邏輯上來講可以這麼想,先確定style,再給其賦值frame。
    
    再看看通過xib建立自訂cell
    代碼:
         testCell * cell = [[[NSBundle mainBundle] loadNibNamed:@"testCell" owner:nil options:nil] firstObject];
    控制台:
     2015-12-31 15:34:04.042測試代碼[1264:393941] -[testCell initWithCoder:]
     2015-12-31 15:34:04.042測試代碼[1264:393941] -[testCell awakeFromNib]
    
    結論四:通過xib建立cell不會調用initWithStyle:reuseIdentifier方法,因為xib裡面已經定義好了cell的style,邏輯上可以這麼理解。同樣也先自動調用了initWithCoder,再調用awakeFromNib方法。
    
    閑來蛋疼,寫寫部落格,希望能協助讀者更深的瞭解到建立控制項是系統會自動調用的方法。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.