zoukankan      html  css  js  c++  java
  • 61.初始化方法

        

        1.最常用的初始化
    /**

     初始化方法

      @param type 类型

     @return 对象

     */

    - (instancetype)initWithType:(EnrollOrderViewControllerType)type

    {

        self = [super init];

        if(self)

        {

            self.type = type;

        }

        return self;

    }

    2. 

    initWithFrame:frame //纯代码初始化,由用户调用
    - (id)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code
    }
    return self;
    }
    //当代码中没有用nib时,可以用 if (self =[superinitWithFrame:frame])作为条件限制
    //对init:可以自定义方法做初始化
    - (instancetype)initWithFrame:(CGRect)frame relCourse:(fRelCourse *)item publicCourse:(NSString *)publicCourse{
    if (self = [super initWithFrame:frame]){
    self.relCourseItem = item;
    self.publicCourse = publicCourse;
    [self initSubViews];
    }
    return self;
    }


    3.
    initWithcoder:aDecoder //从nib中加载对象实例时,由框架调用的
    - (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
    // Initialization code
    }
    return self;
    }
    //用nib加载对象实例时:用 if (self =[super initWithcoder:aDecoder])判断,也可以自己添加初始化条件
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
    if ((self = [super initWithCoder:aDecoder]))
    {
    [self setUp];
    }
    return self;
    }
    4. 
    awakeFromNib //通过nib文件创建view对象时执行awakeFromNib ,此方法在initWithCoder:之后,在nib文件被加载时调用,此时可以对一些属性重新赋值
    - (void)awakeFromNib {
    [super awakeFromNib];
    //重新赋值一些控件属性
    }
    5.
    initWithNibName: //是延迟加载,这个View上的控件是 nil ,只有到 需要显示时,才会不是 nil
    //(默认init方法使用)当需要在init中添加条件时可以重写此方法
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    _isPush = YES;
    }
    return self;
    }
    //再通过 initWithNibName来实例化就会默认添加上了 self.isPush = YES;
    SelectViewController *selectVC = [[SelectViewController alloc] initWithNibName:nil bundle:nil];


    6.
    initWithStyle:style reuseIdentifier:reuseIdentifier //指定初始化(一般用作cell中),如果单元格可以复用,则使相同形式的单元格使用相同的重用标识符。
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self loadSubViews]; //添加cell中要加载的控件
    }
    return self;
    }
    //对应的不从xib加载cell 可以选择使用 awakeFromNib


    7.
    loadNibNamed方法:即时加载,用该方法加载的xib对象中的各个元素都已经存在(cell中用xib拖入的控件)
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *VideoCellID = @"VideoCellID"; //重用标示符
    VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:VideoCellID];
    if (cell == nil) {
    cell = [[NSBundle mainBundle] loadNibNamed:@"VideoCell" owner:nil options:nil].lastObject; //加载的cell是VideoCell文件中的最后一个xib
    }
    }




  • 相关阅读:
    IOC(控制反转)
    JQuery中的DOM操作
    【JQuery的选择器】
    JPA基本注解介绍
    JPA使用的HelloWorld
    JPA的介绍
    JQuery简介及HelloWorld
    SpringMvc处理post请求乱码的filter
    Sping3.0版本+Quartz完成定时任务
    CentOS下安装Subversion (SVN)
  • 原文地址:https://www.cnblogs.com/qiangzheVSruozhe/p/9402475.html
Copyright © 2011-2022 走看看