问题描述
我正在听关于 iTunes 的斯坦福 iPhone 开发讲座,并在第 5 讲中遇到了这个问题.
I am going through the Stanford iPhone dev lectures on iTunes and ran into this in Lecture 5.
我们正在努力确保在设备旋转时进行重绘.我有两个与此相关的问题:
We are trying to ensure a redraw will be done when the device rotates.I have two questions related to this:
- 什么是
awakeFromNib
?在其余代码中没有调用此方法.它是如何触发的? initwithFrame:
里面的代码有什么作用?
- What is
awakeFromNib
? There's no call to this method in the rest of the code. How was it triggered? - What does the codes inside
initwithFrame:
do?
.
-(void)setup
{
self.contentMode = UIViewContentModeRedraw;
}
-(void)awakeFromNib
{
[self setup];
}
-(id)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
推荐答案
awakeFromNib
在完成加载笔尖时由 NSBundle
调用.
awakeFromNib
is called by NSBundle
when it finishes loading your nib.
您的代码在初始化视图时实际上有两种不同的代码路径,具体取决于它是从笔尖加载还是在运行时创建的.
You've actually got two different code paths your code can take when initializing a view, depending on whether it's loaded from a nib or created at runtime.
如果它是从笔尖加载的,加载的一部分将通过调用
initWithCoder:
初始化它,然后在所有出口之后调用awakeFromNib
已连接.
If it's loaded from a nib, part of the loading will initialize it by calling
initWithCoder:
, followed by a later call ofawakeFromNib
after all the outlets have been connected.
如果以编程方式创建视图,则使用 initWithFrame:
初始化它(并且 awakeFromNib
永远不会被调用,因为它不是从笔尖加载的).
If you create the view programmatically, you initialize it with initWithFrame:
instead (and awakeFromNib
is never called because it wasn't loaded from a nib).
这篇关于解释awakeFromNib和initwithFrame:用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!