ios20--xib2
故事板控制器:
//
// 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的更多相关文章
- react native初步常见问题
首先按照资料一步步搭建环境运行,然后成功了,很激动,可是,安卓就是没这么容易成功,还是太年轻了 could not get batchedbridge, make sure your bundle i ...
- SQLite3
记录一个基础的IOS下SQLite的例子: @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSAr ...
- PLSQL_性能优化系列11_Oracle Bulk Collect批处理
2014-10-04 Created By BaoXinjian
- xcode xib 加载 、注意点
加载xib2中方式 NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"xib名称" owner:nil options: ...
- IOS UI 第五篇:基本UI
添加个导航栏: Xib1 *xib1 = [[Xib1 alloc] initWithNibName:@"Xib1" bundle:nil]; UINavig ...
- IOS UI 第四篇:基本UI
ViewController 应用 再第一个XIB页面创建另一个XIB页面,并且通过按钮调用它 - (IBAction)GoSecond:(id)sender { secondVie ...
- iOS开发 -------- 图片浏览器初步
一 示例代码 // // RootViewController.m // 图片浏览器初步 // // Created by lovestarfish on 15/11/1. // Copyright ...
- iOS开发 -------- transform属性(形变)
一 transform属性 在OC中,通过transform属性可以修改对象的平移,比例和旋转角度 常用的创建transform结构体的方法分两大类 (1) 创建"基于控件初始位置&qu ...
- ios19---xib
// // ViewController.m #import "ViewController.h" @interface ViewController () @end @imple ...
随机推荐
- Failed to resolve filter报错原因
问题 页面写过滤器,控制台报错,Failed to resolve filter 分析 语法错误?先检查 ``` {{ params | filterA }} filters: { filterA: ...
- 原生j获取元素的几种方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C++中的初始化列表
C++11扩大了列表初始化的适用范围,使其可以用于所有内置类型和用户定义的类型(即类对象). 1.当列表初始化用于内置类型的变量时,这种初始化形式有一个重要的特点:如果我们使用列表初始化且初始化值存在 ...
- 代理模式精讲(手写JDK动态代理)
代理模式是一种架构型模式,表现出来就是一个类代表另一个类的功能,一般用在想对访问一个类的时候做一些控制,同时又不想影响正常的业务,这种代理模式在现实的生活中应用的也非常的广泛,我用穷举法给举几个好理解 ...
- Spring中操作日志记录web请求的body报文
在spring中,通常可以使用切面编程方式对web请求记录操作日志.但是这种方式存在一个问题,那就是只能记录url中的请求参数,无法记录POST或者PUT请求的报文体,因为报文体是放在request对 ...
- buf.entries()详解
buf.entries() 返回:{Iterator} 从当前 Buffer 的内容中,创建并返回一个 [index, byte] 形式的迭代器. const buf = Buffer.from('b ...
- jquery点击事件
$("#hoetelNameSelect").click( $.post("${ctx}/meeting/restaurant/queryHotelName", ...
- Vue如何点亮多个tab选项简易方法
我们平常遇到点击选择会遇到单选或多选,当你设计图是自定义的时候,第一反应就是引入UI插件吧.可是如果项目只用到插件的这一点点功能,我们引入了一个插件这就不太友好了.此时我们自己写这个小功能是很简单的, ...
- 《C语言程序设计(第四版)》阅读心得(四 文件操作)
第10章 对文件的输入输出 函数名 调用形式 功能 fopen fopen(“a1”,”r”); 打开一个文件 fclose fclose( fp ); 关闭数据文件 fgetc fgetc( fp ...
- POJ1422 Air Raid
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8006 Accepted: 4803 Description Consi ...