• 通过Xib自定义控件


    加载xib的两种方式

    1. NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"test" owner:nil options:nil];
    2. UINib *nib = [UINib nibWithNibName:@"Test" bundle:nil];   // nil 默认是mainBundle

      NSArray *array = [nib instantiteWithOwer:nil object:nil];

    控制器拿到xib中控件的方法:

    • 遍历所有的子控件
      // 直接遍历子控件设置数据
    
     for (UIView *view in shopView.subviews) {
    
            if ([view isKindOfClass:[UIImageView class]]) {
    
                UIImageView *imageView = (UIImageView *)view;
    
                imageView.image = [UIImage imageNamed:shop.icon];
    
            } else if ([view isKindOfClass:[UILabel class]]) {
    
                UILabel *label = (UILabel *)view;
    
                label.text = shop.name;
    
            }
    
        }
    • 绑定tag
    // 通过tag拿到对应的子控件设置数据
    
        UIImageView *iconImageView = (UIImageView *)[shopView                  viewWithTag:1];
    
        iconImageView.image = [UIImage imageNamed:shop.icon];
    
        
    
        UILabel *nameLabel = (UILabel *)[shopView viewWithTag:2];
    
        nameLabel.text = shop.name;

    自定义xib的步骤:

    1. 创建一个ShopView,xib和其文件名保持一致(名字随便取,建议有意义一点)
    2. 在Xib中,xib的类从UIView改成ShopView
    3. 给内部的子控件设置数据
    4. 提供类方法封装xib的加载过程
    + (instancetype)shopView
    
    {
    
        return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
    
    }
    
    + (instancetype)shopViewWithShop:(Shop *)shop
    
    {
    
       //创建shopView 
      ShopView
    *shopView = [self shopView];
      //给shopView设置数据   shopView.shop
    = shop;   return shopView; }

    xib的加载原理

    • 一层一层的解析转为对应的代码
    - (void)loadXib
    
    {
    
        XMGShopView *shopView = [[XMGShopView alloc] initWithCoder:nil];
    
        shopView.frame = CGRectMake(0, 0, 70, 90);
    
        shopView.backgroundColor = [UIColor whiteColor];
    
        
    
        UIImageView *iconImageView = [[UIImageView alloc] initWithCoder:nil];
    
        iconImageView.backgroundColor = [UIColor greenColor];
    
        iconImageView.frame = CGRectMake(0, 0, 70, 70);
    
        [shopView addSubview:iconImageView];
    
        self.iconImageView = iconImageView;
    
        
    
        XMGLabel *nameLabel = [[XMGLabel alloc] initWithCoder:nil];
    
        nameLabel.backgroundColor = [UIColor greenColor];
    
        nameLabel.frame = CGRectMake(0, 0, 70, 70);
    
        [shopView addSubview:nameLabel];
    
        self.nameLabel = nameLabel;    
    
    }
    • xib中的控件是什么类型,加载出来就是什么类型
    • xib中的子控件是什么类型,脱线出来就是什么类型
    • 通过alloc/init或者alloc/initWithFrame创建控件不会主动加载xib,即使xib的名称和控件的类名一样

    注意点:

    • 如果是通过代码创建的控件,初始化时一定会调用initWithFrame
    • 如果是通过xib或者storyboard创建控件,初始化时是不会调用initWithFrame,会调用initWithCoder
    • 如果是通过xib或者storyboard创建控件,初始化完毕后会调用awakeFromNib方法
    • 建议在awakeFromNib中做初始化
  • 相关阅读:
    泰勒综合
    滤波器、窗等的系数为什么是对称的?
    l'alphabet en francais
    弄清for循环的本质
    js中的闭包
    js中用正则表达式
    java Calendar
    Android实现XML解析技术
    junit4 详解
    redhat vi 命令
  • 原文地址:https://www.cnblogs.com/wwjwb/p/12650271.html
Copyright © 2020-2023  润新知