标签:初始 调用 名称 imageview ima array obj nsa bsp
加载xib的两种方式
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拿到对应的子控件设置数据 UIImageView *iconImageView = (UIImageView *)[shopView viewWithTag:1]; iconImageView.image = [UIImage imageNamed:shop.icon]; UILabel *nameLabel = (UILabel *)[shopView viewWithTag:2]; nameLabel.text = shop.name;
自定义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; }
注意点:
标签:初始 调用 名称 imageview ima array obj nsa bsp
原文地址:https://www.cnblogs.com/wwjwb/p/12650271.html