代码初始化 故事板初始化 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初始化的方法就在相关联的类中
 
原文地址:https://www.cnblogs.com/apple-image/p/5345587.html