zoukankan      html  css  js  c++  java
  • initWithNibName

    第一、initWithNibName这个方法是在controller的类在IB中创建,但是通过Xcode实例化controller的时候用的.
    第 二、initWithCoder 是一个类在IB中创建但在xocde中被实例化时被调用的.比如,通过IB创建一个controller的nib文件,然后在xcode中通过 initWithNibName来实例化这个controller,那么这个controller的initWithCoder会被调用.或者是一个 view的nib文件,类似方法创建时调用initWithCoder
    第三、awakeFromNib

    当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的awakeFromNib函数来响应这个消息,执行一些必要的操作。也就是说通过nib文件创建view对象时执行awakeFromNib
    第四、关于 initWithNibName 和 loadNibNamed 的区别和联系 :

    关于 initWithNibName 和 loadNibNamed 的区别和联系。之所以要把这两者来一起讲,我觉的我也有点困惑,到底用那种?其实真正搞清楚了他们之间的差别,就不会这么迷惘了。因为这两个方法,根本就不是一路货色。
    既然,是要说明这2个方法,那就着重将区别吧。
    但是第一步,还是要罗嗦一下,他们的联系:可以使用此方法加载用户界面(xib文件)到我们的代码中,这样,可以通过操作这个加载进来的(xib)对象,来操作xib文件内容。
    下面进入主题,谈区别:
    1. ShowViewController的initWithNibName方法
    ShowViewController * showMessage = [[ShowViewController alloc]
    initWithNibName:@"ShowViewController" bundle:nil];
    self.showViewController = showMessage;
    [showMessage release];
    2.VideoCellController的loadNibNamed方法
    NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"Save3ViewController"
    owner:self options:nil] ;
    总结:
    只看他们初始化,那可能感觉是一样的。但是如果,打开分别看xib的关系的时候,才恍然大悟,原来他们的集成类都不一样。
    1. initWithNibName要加载的xib的类为我们定义的视图控制器类
    2.加载方式不同
    initWithNibName方法:是延迟加载,这个View上的控件是 nil 的,只有到 需要显示时,才会不是 nil
    loadNibNamed方法:即时加载,用该方法加载的xib对象中的各个元素都已经存在。
    (认 真理解这句帮规:when using loadNibNamed:owner:options:, the File's Owner should be NSObject, the main view should be your class type, and all outlets should be hooked up to the view, not the File's Owner.)

    第五、initWithCoder和initWithFrame的区别

    nitWithoder 是当从nib文件中加载对象的时候会调用,比如你的view来自nib那么就会调用这个view的这个函数。(由框架调用)
    initWithFrame (是由用户调用,来初始化对象的)

    The init method that gets used depends on how the view is created. It can be explicitly created using initWithFrame or it can be created by loading a nib. In that case, the initWithCoder method gets called when the view is loaded from the nib. There are other init methods for subclasses (like UITableViewController has initWithStyle), so you have to be sure which one is being called.

    self.showViewController = [nib lastObject];
    [nib objectAtIndex:0]; 

  • 相关阅读:
    iOS 动画总结UIView动画
    iPhone 本地通知
    NSNotification学习笔记
    [重构]把程序写得更简洁,更好维护
    使用asp:Timer控件为站点创建一个实时时钟
    为用户控件(UserControl)写属性
    Gridview前面10行数据显示背景色
    MS SQL获取最大值或最小值日期的函数
    How to modify Inventory Aging Report form days field default value
    DropDownlist的DataTextField显示多列数据
  • 原文地址:https://www.cnblogs.com/hsxblog/p/5104651.html
Copyright © 2011-2022 走看看