iPhone用nib/xib文件载入窗口,和用代码写窗口,到底哪个快

iPhone用nib/xib文件载入窗口,和用代码写窗口,到底哪个快?
有人认为iPhone上用代码来构建加载窗口要比用nib文件来得更快。真的假的?下面文章做了一个实验,说明了这一问题:
这篇文章使用的Sample程序非常简单:一个包含了20行Cell的UITableview,每一行Cell又包含了20个UILabel,1个backgroundView,还有一个selectedBackgroundView。
计时只是在构建和加载的时候进行。像把cell加进UITableView、配置cell、屏幕绘制的过程没有进行计时,因为不管用代码还是用nib,这些过程都会有。
上图右手边,每个cell上的黑线是”placeholder”,被写了19次。
你可以工程:工程文件
创建Cell的代码
创建一个cell的代码如下:
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.backgroundView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1.0];
UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 60, 20)] autorelease];
firstLabel.tag = 1;
firstLabel.font = [UIFont boldSystemFontOfSize:14];
firstLabel.shadowOffset = CGSizeMake(1,1);
firstLabel.textColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.5 alpha:1.0];
firstLabel.backgroundColor = [UIColor clearColor];
firstLabel.text = @"placeholder";
firstLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftM
firstLabel.adjustsFontSizeToFitWidth = YES;
firstLabel.minimumFontSize = 10;
firstLabel.baselineAdjustment = UIBaselineAdjustmentAlignC
firstLabel.lineBreakMode = UILineBreakModeTailT
firstLabel.baselineAdjustment = UIBaselineAdjustmentAlignC
firstLabel.highlightedTextColor = [UIColor clearColor];
[cell addSubview:firstLabel];
// // Plus the construction of a further 19 labels... //
cell = [[[UITableViewCell
alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier]
autorelease];
cell.backgroundView =
[[[UIView alloc]
initWithFrame:CGRectZero]
autorelease];
cell.backgroundView.backgroundColor =
[UIColor colorWithWhite:0.95
alpha:1.0];
cell.selectedBackgroundView =
[[[UIView alloc]
initWithFrame:CGRectZero]
autorelease];
cell.selectedBackgroundView.backgroundColor =
[UIColor colorWithWhite:0.85
alpha:1.0];
UILabel *firstLabel =
[[UILabel alloc]
initWithFrame:CGRectMake(5,
autorelease];
firstLabel.tag =
firstLabel.font =
[UIFont boldSystemFontOfSize:14];
firstLabel.shadowOffset =
CGSizeMake(1,1);
firstLabel.textColor =
[UIColor colorWithRed:0.0
alpha:1.0];
firstLabel.backgroundColor =
[UIColor clearColor];
firstLabel.text =
@"placeholder";
firstLabel.autoresizingMask =
UIViewAutoresizingFlexibleLeftM
firstLabel.adjustsFontSizeToFitWidth =
firstLabel.minimumFontSize =
firstLabel.baselineAdjustment =
UIBaselineAdjustmentAlignC
firstLabel.lineBreakMode =
UILineBreakModeTailT
firstLabel.baselineAdjustment =
UIBaselineAdjustmentAlignC
firstLabel.highlightedTextColor =
[UIColor clearColor];
[cell addSubview:firstLabel];
// // Plus the construction of a further 19 labels... //
加载nib文件
有很多方法可以从nib文件中加载一个UITableViewCell。这里使用一个最快最简单的方法:
[[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = loadedC
loadedCell =
[[NSBundle mainBundle]
loadNibNamed:@"Cell"
owner:self options:nil];
cell = loadedC
loadedCell =
模拟器结果
Generated in code
Loaded from NIB
Generated cell in 0. seconds
Loaded cell in 0.00184 seconds
Generated cell in 0. seconds
Loaded cell in 0. seconds
Generated cell in 0. seconds
Loaded cell in 0. seconds
Generated cell in 0. seconds
Loaded cell in 0.001706 seconds
Generated cell in 0. seconds
Loaded cell in 0.001697 seconds
Generated cell in 0. seconds
Loaded cell in 0. seconds
Generated cell in 0. seconds
Loaded cell in 0.002105 seconds
Generated cell in 0. seconds
Loaded cell in 0. seconds
Generated cell in 0. seconds
Loaded cell in 0. seconds
Generated cell in 0. seconds
Loaded cell in 0.001692 seconds
通过nib文件的方式加载比用代码加载慢了将近20%。但是,每次加载只是慢1毫秒左右,关系并不是很大。
真机测试结果
下面这个表是用iPhone 3G测试的:
Generated in code
Loaded from NIB
Generated cell in 0.113011 seconds
Loaded cell in 0.131085 seconds
Generated cell in 0.114312 seconds
Loaded cell in 0.097244 seconds
Generated cell in 0.101614 seconds
Loaded cell in 0.08413 seconds
Generated cell in 0.105022 seconds
Loaded cell in 0.081331 seconds
Generated cell in 0.10087 seconds
Loaded cell in 0.093407 seconds
Generated cell in 0.105968 seconds
Loaded cell in 0.083472 seconds
Generated cell in 0.100045 seconds
Loaded cell in 0.091788 seconds
Generated cell in 0.105458 seconds
Loaded cell in 0.083763 seconds
Generated cell in 0.098836 seconds
Loaded cell in 0.08714 seconds
Generated cell in 0.102028 seconds
Loaded cell in 0.109811 seconds
从上面结果上来看,构建第一个cell时代码的方式要快15%,但是从第三个cell开始nib方式要快17%。
用CPU sampling的工具测试了一下。发现adjustsFontSizeToFitWidth 的方法比较慢。这个方法是Interface Builder用来预先计算字的大小的。我们不要用这个方法。修改代码和nib文件(label.adjustsFontSizeToFitWidth = NO、使用Cell2.xib)后,运行得到下面的结果:
Generated in code
Loaded from NIB
Generated cell in 0.085553 seconds
Loaded cell in 0.095012 seconds
Generated cell in 0.077257 seconds
Loaded cell in 0.087141 seconds
Generated cell in 0.084639 seconds
Loaded cell in 0.082693 seconds
Generated cell in 0.079142 seconds
Loaded cell in 0.098218 seconds
Generated cell in 0.078286 seconds
Loaded cell in 0.082136 seconds
Generated cell in 0.087895 seconds
Loaded cell in 0.087088 seconds
Generated cell in 0.0792 seconds
Loaded cell in 0.082335 seconds
Generated cell in 0.084037 seconds
Loaded cell in 0.082358 seconds
Generated cell in 0.076416 seconds
Loaded cell in 0.08714 seconds
Generated cell in 0.078426 seconds
Loaded cell in 0.084312 seconds
现在,代码方式快了7%。
不要做出这样的假设,nib文件总是比写代码的方式慢。在一般情况下,使用代码方式生成view比用nib的方式快5%~10%。使用nib文件虽然比较慢,但是差别非常小,没有关系。况且有时候用nib文件要比用代码来得快。
这并不意味着在iphone中UI的速度无关紧要。我曾经写过加载要用2秒的view。这是不能接受的。但是节省10%的nib时间,也不会解决这个问题。在这种情况下,减少view深度,或将text field删除是优化性能的最好方法。这样做能让程序快上10倍或者更多。
用还是不用nib,完全看你的选择。原则就是自己coding舒服并且程序没有性能问题。不要太在意这两个方法的性能差异。
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'Pages: 1/2
主题 : xib/nib文件转换为objective c代码
级别: 圣骑士
UID: 65157
可可豆: 3436 CB
威望: 3376 点
在线时间: 111(时)
发自: Web Page
来源于&&分类
xib/nib文件转换为objective c代码&&&
这个牛人写的,感觉很酷,附件中直接是我生成好的app,可以下载试用~如果有对代码有兴趣的,可以去这里看注:代码引用到这个库:
(660 K) 下载次数:452
级别: 圣骑士
UID: 65157
可可豆: 3436 CB
威望: 3376 点
在线时间: 111(时)
发自: Web Page
自己沙发了 哈哈
级别: 新手上路
可可豆: 348 CB
威望: 348 点
在线时间: 51(时)
发自: Web Page
那个怎么用类?
级别: 圣骑士
UID: 65157
可可豆: 3436 CB
威望: 3376 点
在线时间: 111(时)
发自: Web Page
回 2楼(aipame) 的帖子
打开APP,点击“file”--&&open...&--&选择xib文件,然后就会弹出一个窗口啦!
级别: 侠客
可可豆: 1095 CB
威望: 1095 点
在线时间: 3(时)
发自: Web Page
谢谢楼主了~~
级别: 精灵王
发帖: 1406
可可豆: 13025 CB
威望: 12885 点
在线时间: 207(时)
发自: Web Page
学习了,谢谢楼主~~~
级别: 新手上路
可可豆: 348 CB
威望: 348 点
在线时间: 51(时)
发自: Web Page
多谢楼主 教导!!!!!!!!!
级别: 侠客
可可豆: 1222 CB
威望: 1222 点
在线时间: 66(时)
发自: Web Page
先收藏了,留着以后用。
级别: 新手上路
可可豆: 73 CB
威望: 73 点
在线时间: 383(时)
发自: Web Page
mark一下.......
级别: 圣骑士
UID: 29429
可可豆: 2928 CB
威望: 2875 点
在线时间: 509(时)
发自: Web Page
回 3楼(nick_jackson) 的帖子
为何我打开里面全部都是空的?
Pages: 1/2
关注本帖(如果有新回复会站内信通知您)
8*2-5 正确答案:11
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版  首先新建一个继承自UIView的类,并创建相应的XIB文件
#import &UIKit/UIKit.h&
@interface ZHDemoView : UIView
+ (instancetype)demoV
  提供一个类方法以供快速创建当前类
  在.m中实现类方法,通常从XIB中加载出来的是一个数组,所以用到了lastObject方法
+ (instancetype)demoView
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"ZHDemoView" owner:nil options:nil];
return [nibArray lastObject];
  在awakeFromNib中添加相应的手势
- (void)awakeFromNib
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
[self addGestureRecognizer:tap];
- (void)click
NSLog(@"%s", __func__);
  另外,测试用的XIB长这样:
  在根控制器添加一个这样的View,运行并点击,结果是
2015-08-11 13:44:44.988 XIB注意[1797:362096] -[ZHDemoView click]
  一切正常,接下来我们不在代码中添加手势,直接在XIB中添加(之前添加手势的代码已清除):
  运行结果:
2015-08-11 14:02:12.747 XIB注意[1962:374487] -[UITapGestureRecognizer superview]: unrecognized selector sent to instance 0x7fe47af49c60
  可以看到是消息发送错误,UITapGestureRecognizer中没有superview方法。
  回到这张图
  我们可以看到Objects栏下方有手势识别器和DemoView两个对象,那么是否有可能是因为XIB加载的对象错误呢?
  因为之前提供的类方法中我们使用的是lastObject方法,这次我们使用firstObject试一试
  更改类方法如下:
+ (instancetype)demoView
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"ZHDemoView" owner:nil options:nil];
return [nibArray firstObject];
  结果:
2015-08-11 14:13:16.847 XIB注意[2064:382058] -[ZHDemoView click:]
  一切正常。
  从XIB加载出来的是一个对象数组,通常情况下,我们只需要用到一个对象,此时用firstObject或者lastObject皆可。但是当你要使用手势识别器时,别弄错你取出的对象。建议不要在XIB中直接添加手势识别器,通过代码手动创建更稳妥和易于维护。
阅读(...) 评论()主题 : 关于“怎样读取不同的xib文件”真不会
级别: 侠客
UID: 45045
可可豆: 313 CB
威望: 302 点
在线时间: 723(时)
发自: Web Page
来源于&&分类
关于“怎样读取不同的xib文件”真不会&&&
最近在做iphone5的适配,在论坛里看到高手们总结了很多办法,但是最后有一句“怎样读取不同的xib文件不用上代码了吧”,可这个以前真没做过,实在是找不到资料了,哪位好心人给贴一两行代码看看到底怎么读取不同的xib文件吧,万分感谢!!!另外,用同样的办法可以适配iPad吗?也请好心高手指点指点啊
级别: 新手上路
可可豆: 7 CB
威望: 7 点
在线时间: 9(时)
发自: Web Page
同问。。。。
级别: 精灵王
UID: 78241
可可豆: 3741 CB
威望: 3824 点
在线时间: 1012(时)
发自: Web Page
initWithNibName:(NSString *) bundle:(NSBundle *),初始化的时候判断是iphone5还是其他的,然后nibName取不同的,每个xib需要设置一个identifier
关注本帖(如果有新回复会站内信通知您)
3*3+1 正确答案:10
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版

我要回帖

更多关于 ios nib xib 的文章

 

随机推荐