解释awakeFromNib和initwithFrame:用法 [英] Explain awakeFromNib and initwithFrame: usage

查看:40
本文介绍了解释awakeFromNib和initwithFrame:用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在听关于 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:

  1. 什么是awakeFromNib?在其余代码中没有调用此方法.它是如何触发的?
  2. initwithFrame: 里面的代码有什么作用?
  1. What is awakeFromNib? There's no call to this method in the rest of the code. How was it triggered?
  2. 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 of awakeFromNib 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:用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆