[UIKit学习]08.关于自定义控件

自定义控件

选用xib用自定义view代码与xib相关联

示例代码

+ (instancetype)shopView

{

    

    return [self shopViewWithShop:nil];

}

 

+ (instancetype)shopViewWithShop:(XMGShop *)shop

{

//*注:自定义控件时,尽量把所有关于控件本身逻辑在控件本身处理

    XMGShopView *shopView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];

    shopView.shop = shop;

    return shopView;

}

 

- (void)setShop:(XMGShop *)shop

{

    _shop = shop;

    

    // 设置子控件的数据

//    UIImageView *iconView = [self viewWithTag:10];

//    UIImageView *iconView = [self.subviews firstObject];

    self.iconView.image = [UIImage imageNamed:shop.icon];

    

//    UILabel *nameLabel = [self viewWithTag:20];

//    UILabel *nameLabel = [self.subviews lastObject];

    self.nameLabel.text = shop.name;

}