代碼初始化 故事板初始化 xib初始化總結

對象的初始化有三種方式
 
// 代碼建立
- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
       
       
        self.backgroundColor = [UIColor clearColor];
        [self creatKits];//加載子控件
    }

    return self;
}
 
 
 
//經過Xib加載初始化文件(建立xib文件的只是一個視圖,沒有控制器,把xib文件和視圖相關聯)
  _headerView = [[[NSBundle mainBundle] loadNibNamed:@"HomeHeaderView" owner:nil options:nil] lastObject];
 
 
//經過xib文件加載
- (void)awakeFromNib {

    [super awakeFromNib];
   
    self.backgroundColor = [UIColor clearColor];
    [self creatKits];

}
 
//注意當建立了xib 文件可是不經過沙盒路徑去讀取,而是用xib視圖上的某些控件能夠直接用拖拽便可(這種狀況只有視圖控制器控制器能夠,若是是視圖類不拖拽是不行的);
 
 
 
 
 
 
 
 
 
 
 
 
 
//UIStoryboard初始化的(建立的故事版是一個控制器 UIStoryboard)
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"ImageNewsStoryboard" bundle:[NSBundle mainBundle]];
       
        ImageNewsViewController *vc = [sb instantiateInitialViewController];
       
  
        [self.viewController.navigationController pushViewController:vc animated:YES];
 
 
// 經過storyBoard建立
- (id)initWithCoder:(NSCoder *)aDecoder {

    if (self = [super initWithCoder:aDecoder]) {
       
        self.backgroundColor = [UIColor clearColor];
        [self creatKits];
    }
    return self;
}
 
//注意是事項:
  經過Xib加載初始化的同時會走以其相關聯的class類初始化複寫xib初始化的方法就在相關聯的類中
經過Storyboard加載初始化的同時會走以其相關聯的class類初始化複寫Storyboard初始化的方法就在相關聯的類中
相關文章
相關標籤/搜索