故事板控制器:

//
// 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

ios20--xib2的更多相关文章

  1. react native初步常见问题

    首先按照资料一步步搭建环境运行,然后成功了,很激动,可是,安卓就是没这么容易成功,还是太年轻了 could not get batchedbridge, make sure your bundle i ...

  2. SQLite3

    记录一个基础的IOS下SQLite的例子: @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSAr ...

  3. PLSQL_性能优化系列11_Oracle Bulk Collect批处理

    2014-10-04 Created By BaoXinjian

  4. xcode xib 加载 、注意点

    加载xib2中方式 NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"xib名称" owner:nil options: ...

  5. IOS UI 第五篇:基本UI

    添加个导航栏:         Xib1 *xib1 = [[Xib1 alloc] initWithNibName:@"Xib1" bundle:nil];    UINavig ...

  6. IOS UI 第四篇:基本UI

    ViewController 应用   再第一个XIB页面创建另一个XIB页面,并且通过按钮调用它     - (IBAction)GoSecond:(id)sender {    secondVie ...

  7. iOS开发 -------- 图片浏览器初步

    一 示例代码 // // RootViewController.m // 图片浏览器初步 // // Created by lovestarfish on 15/11/1. // Copyright ...

  8. iOS开发 -------- transform属性(形变)

      一 transform属性 在OC中,通过transform属性可以修改对象的平移,比例和旋转角度 常用的创建transform结构体的方法分两大类 (1) 创建"基于控件初始位置&qu ...

  9. ios19---xib

    // // ViewController.m #import "ViewController.h" @interface ViewController () @end @imple ...

随机推荐

  1. nodejs运行机制

    有一天老大忽然问起我这个问题,nodejs运行机制 是怎样的?因自己对nodejs也不是很熟悉,就上网查了一下,得出结果如下: 1.简介 Node.js是一个事件驱动I/O服务端JavaScript环 ...

  2. ProtoBuf - Arena

    1.概述 最近看 Protocal Buffer 的源码,初次见到这个库源自陈硕的 muduo ,便打算看一看,在此做一下记录.官网文档不能访问,只能凭借代码的自己理解,查看的源码版本为 3.6.0. ...

  3. CentOS7-wget命令

    Wget主要用于下载文件,在安装软件时会经常用到,以下对wget做简单说明.转载自:https://www.cnblogs.com/lxz88/p/6278268.html 1.下载单个文件:wget ...

  4. Java核心技术卷1 第三章

    1. Java区分大小写,下一段源代码中,关键字public称为访问修饰符,用于控制程序的其他部分对于这段代码的访问级别,关键字class表明Java程序中的全部内容都包含在类里面. 标准的类名命名规 ...

  5. LINUX:Contos7.0 / 7.2 LAMP+R 下载安装Redis篇

    文章来源:http://www.cnblogs.com/hello-tl/p/7569108.html 更新时间:2017-09-21 16:09 简介 LAMP+R指Linux+Apache+Mys ...

  6. PDO、PDOStatement、PDOException

    最近在学PDO  比较详细的资料 出处:http://blog.csdn.net/hsst027/article/details/23682003 PDO中包含三个预定义的类,它们分别是PDO.PDO ...

  7. Linux虚拟机安装学习笔记

    一.Linux系统的安装1.VMwaer虚拟机的安装使用 官方下载软件地址:www.vmwaer.com 安装的虚拟机可以与现实的计算机进行通信 安装虚拟主机可以随意定制硬件安装配置建议: CPU:1 ...

  8. 【转】Java读写文件大全

    使用Java操作文本文件的方法详解        最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(ch ...

  9. [Wf2015]Tours

    [Wf2015]Tours 题目 给定一张n个点m条边的无向图,你需要选择一个颜色种类数k,然后用这k种颜色给每条边染色,要求对于图中任意一个简单环,每种颜色的边的数量都相同,求所有可行的k INPU ...

  10. bzoj 3173 [Tjoi2013]最长上升子序列 (treap模拟+lis)

    [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2213  Solved: 1119[Submit][Status] ...