故事板控制器:

//
// 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. Python 1-3区分Python文件的两种用途和模块的搜索路径

    区分Python文件的两种用途 run.py文件: import m1 m1.py文件: def f1(): print('f1') def f2(): print('f2') #当文件被执行时__n ...

  2. 利用WITH AS改写SQL

    报表程序中一段SQL语句. 优化前: 返回:3952 耗时:224s SQL 代码: select to_date(nvl(pro.value, '1900-01-01 00:00:00'), 'YY ...

  3. ubuntu 虚拟机系统调优

    Ubuntu虚拟机镜像最佳实践 分区/boot     >1G/root      >10G/var        >5G配swap空间,内存的2倍 vi    /etc/secur ...

  4. Python基础—线程、进程和协程

    今天已是学习Python的第十一天,来干一碗鸡汤继续今天的内容,今天的鸡汤是:超越别人对你的期望.本篇博客主要介绍以下几点内容: 线程的基本使用: 线程的锁机制: 生产者消费之模型(队列): 如何自定 ...

  5. flash-热风焊盘的制作

    计算部分: 热风焊盘的内径(ID)等于钻孔直径+20mil, 外径(OD)等于Anti-pad的直径,通常是比焊盘的直径大20mil. 开口宽度等于(OD-ID)/2+10mil,保留整数位. 如果焊 ...

  6. 24L01-2.4G无线传输模块调节记录

    在调试24L01的时候,虽然能用到别人的程序,但仅仅是程序的初始化,并没有告诉我们如何去后续的操作,如何去再次发送一组数.最近调试24L01接近尾声,将逐一的地方总结下来,以便以后查阅,也供其他人借鉴 ...

  7. api安全认证

    三.auth自定义授权 客户端代码: import requests import hashlib import time current_time = time.time() #自意义的字符串app ...

  8. xtu summer individual 6 D - Checkposts

    Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...

  9. Linux环境下使用VSCode编译makefile文件的注意事项

    Linux环境下使用VSCode编译makefile文件的注意事项 首先安装C/C++的两个依赖 在debug,launch会自动的生成下方的launch.json launch.json { // ...

  10. JavaEE JDBC 怎么加载驱动

    JDBC怎么加载驱动 @author ixenos 分析 1.JDBC是一套连接数据库的接口(放在java.util.sql.Driver类中),不同的数据库依此接口各自实现Java连接到数据库的操作 ...