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 ...
随机推荐
- http和socket
大多数情况下都是使用Http协议做网络通信的,少数情况下,如扣扣之类的即时通讯,就是用Socket建立长链接 Http一般都是短连接的,即客户端和服务端通讯一次后,服务端就关闭连接 Socket是长连 ...
- [Python3网络爬虫开发实战] 1.7.3-Appium的安装
Appium是移动端的自动化测试工具,类似于前面所说的Selenium,利用它可以驱动Android.iOS等设备完成自动化测试,比如模拟点击.滑动.输入等操作,其官方网站为:http://appiu ...
- 配置Mysql审计
mysql-audit.json:Mysql审计日志 插件下载地址: https://bintray.com/mcafee/mysql-audit-plugin/release/1.1.4-725#f ...
- socket scoketserver
import socket sk = socket.socket() # 创建了一个socket对象 # sk.setsockopt(socket.SOL_SOCKET,socket.SO_REUSE ...
- 指定PING的网卡
struct ifreq ifr; // 绑定在eth0上 memset( &ifr, , sizeof( struct ifreq ) ); snprintf( ifr.ifr_name, ...
- 杭电1722 Cake (分蛋糕)
#include<cstdio> int f(int m,int n) { ) return n; else return f(n,m%n); } int main() { int m,n ...
- pcb中几个层的解释
阻焊层(Solder Mask):又称为绿油层,是PCB的非布线层,用于制成丝网漏印板,将不需要焊接的地方涂一层阻焊物质,防止焊接PCB时焊锡在高温下的流动性.在阻焊层上预留的焊盘大小,要比实际焊盘大 ...
- noip模拟赛 入阵曲
分析:其实很容易想到O(n^3m^3)的算法,枚举x1,x2,y1,y2,再统计一下和.求和可以用前缀和,能优化到O(n^2m^2),能得到60分.对于特殊性质的点,求一下a[i][j]与k的最小公倍 ...
- [ C语言版 ] 数独计算器 [ 搜索剪枝法 ]
[原创]转载请注明出处. [浙江大学 程序设计专题] 使用方法:按提示输入方式为9*9的矩阵,0表示未知数. 为解决这一问题,我们也尝试了两种方法,准确的说,是第一种方法太慢了,我们对它进行了优化. ...
- fread了解一下
神奇读入挂^_^ 记得加头文件#include const int BufferSize=100*1000; char buffer[BufferSize],*head,*tail; bool not ...