故事板控制器:

//
// 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(, , , ); // 给子控件设置属性
/*
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(@""); }
return self;
} #pragma mark - xib的加载原理
- (UIView *)loadFormNib{//加载应该返回View。
XMGShopView *shopView = [[XMGShopView alloc] initWithCoder:nil];
shopView.frame = CGRectMake(, , , ); UIImageView *iconView = [[UIImageView alloc] initWithCoder:nil];
iconView.backgroundColor = [UIColor greenColor];
iconView.frame = CGRectMake(, , , );
iconView.tag = ;
[shopView addSubview:iconView];
self.iconView = iconView; UILabel *label = [[UILabel alloc] initWithCoder:nil];
label.backgroundColor = [UIColor orangeColor];
label.tag = ;
[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(@"");
} #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

最新文章

  1. 用信息值进行特征选择(Information Value)
  2. bzoj1904: Musical Water-fence
  3. Docker指令集
  4. JSP中显示用户信息
  5. 跟我学STL系列(1)——STL入门介绍
  6. Codeforces Round #385 (Div. 2) B - Hongcow Solves A Puzzle 暴力
  7. UrlRewriter.dll伪静态实现二级域名泛解析
  8. android开发学习笔记:圆角的Button
  9. jbpmAPI-8
  10. c语言,数据类型转换
  11. JavaBean--实例:注册验证
  12. c语言字符相关函数
  13. Linux实操篇
  14. Exception 05 : Could not instantiate id generator
  15. what&#39;s the python之异常处理
  16. 实战--利用HierarchicalClustering 进行基因表达聚类分析
  17. WebGL编程指南案例解析之加载纹理(贴图)
  18. PhotoModeler Scanner教程
  19. c# MVC模式学习笔记_数据验证
  20. Struts2详讲

热门文章

  1. UIScrollView的contentSize、contentOffset和contentInset属性
  2. svn in xcode5
  3. show()的方向
  4. Linux思维导图之网络管理
  5. LVS-NAT负载均衡PHP应用(Wordpress、Discuz)
  6. centos6 磁盘与文件系统管理
  7. win7 x64安装glpk
  8. Python爬虫入门教程: All IT eBooks多线程爬取
  9. 牛客网补题 New Game!(原Wannafly summer camp day2原题)
  10. 【01】什么是AJAX