标签:nslog return com Owner tool uicolor for first 数据
故事板控制器:
// // ViewController.m // 03-通过xib自定义商品的View #import "ViewController.h" #import "XMGShopView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 加载xib // XMGShopView *shopView = [[[NSBundle mainBundle] loadNibNamed:@"XMGShopView" owner:nil options:nil] firstObject]; // XMGShopView *shopView = [[XMGShopView alloc] initWithFrame: CGRectMake(100, 100, 80, 100)]; XMGShopView *shopView = [XMGShopView shopView]; shopView.frame = CGRectMake(100, 100, 80, 100); // 给子控件设置属性 /* UIImageView *imageView = [shopView viewWithTag:100]; UILabel *titleLabel = [shopView viewWithTag:200]; imageView.image = [UIImage imageNamed:@"danjianbao"]; titleLabel.text = @"单肩包"; */ [shopView setName:@"单肩包"]; [shopView setIcon:@"danjianbao"]; [self.view addSubview:shopView]; } @end
xib对应的类:
// // XMGShopView.h #import <UIKit/UIKit.h> @interface XMGShopView : UIView // 提供set方法 - (void)setIcon: (NSString *)icon; - (void)setName: (NSString *)name; // 提供快速创建方法 + (instancetype)shopView; @end
// XMGShopView.m /** xib使用注意事项: 1> 如果一个view从xib中加载,就不能用[xxx alloc] init] 和 [xxx alloc] initWithFrame:]创建 2> 如果一个xib经常被使用,应该提供快速构造类方法 3> 如果一个view从xib中加载: 用代码添加一些子控件,得在 initWithCoder: 和 awakeFromNib 创建 4> 如果一个view从xib中加载,会调用initWithCoder: 和 awakeFromNib,不会调用init和initWithFrame:方法 */ #import "XMGShopView.h" @interface XMGShopView () @property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; /** 测试label */ @property (nonatomic, weak) UILabel *label; /** 毛玻璃 */ @property (nonatomic, weak) UIToolbar *toolBar; @end @implementation XMGShopView /** * 如果View从xib中加载,就不会调用init和initWithFrame:方法 * */ /* - (instancetype)init{ if (self = [super init]) { NSLog(@"%s", __func__); } return self; } - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { NSLog(@"%s", __func__); } return self; } */ /** * 如果View从xib中加载,就会调用initWithCoder:方法 * 创建子控件,... 注意: 如果子控件(UIImageView,UILabel)是从xib中创建,是处于未唤醒状态 */ - (instancetype)initWithCoder:(NSCoder *)aDecoder{ if (self = [super initWithCoder:aDecoder]) { /* UILabel *label = [[UILabel alloc] init]; label.backgroundColor = [UIColor grayColor]; label.text = @"哈哈哈哈哈哈"; [self addSubview:label]; self.label = label; */ NSLog(@"1"); } return self; } #pragma mark - xib的加载原理 - (UIView *)loadFormNib{//加载应该返回View。 XMGShopView *shopView = [[XMGShopView alloc] initWithCoder:nil]; shopView.frame = CGRectMake(0, 0, 80, 100); UIImageView *iconView = [[UIImageView alloc] initWithCoder:nil]; iconView.backgroundColor = [UIColor greenColor]; iconView.frame = CGRectMake(0, 0, 80, 80); iconView.tag = 100; [shopView addSubview:iconView]; self.iconView = iconView; UILabel *label = [[UILabel alloc] initWithCoder:nil]; label.backgroundColor = [UIColor orangeColor]; label.tag = 200; [shopView addSubview:label]; self.titleLabel = label; return shopView; } /** * 从xib中唤醒 添加 xib中创建的子控件 的子控件 */ - (void)awakeFromNib{ // 往imageView上加毛玻璃 UIToolbar *toolBar = [[UIToolbar alloc] init]; [self.iconView addSubview:toolBar]; self.toolBar = toolBar; NSLog(@"2"); } #pragma mark - 快速构造方法 + (instancetype)shopView{ return [[[NSBundle mainBundle] loadNibNamed:@"XMGShopView" owner:nil options:nil] firstObject]; } #pragma mark - 布局子控件 - (void)layoutSubviews{ [super layoutSubviews]; /* self.label.frame = self.bounds; */ self.toolBar.frame = self.iconView.bounds; } #pragma mark - 设置数据 - (void)setIcon:(NSString *)icon{ self.iconView.image = [UIImage imageNamed:icon]; } - (void)setName:(NSString *)name{ self.titleLabel.text = name; } @end
标签:nslog return com Owner tool uicolor for first 数据
原文地址:http://www.cnblogs.com/yaowen/p/7469490.html