init、initWithCoder、initWithFrame、initWithNib

来源:互联网 发布:国外在淘宝买东西 编辑:程序博客网 时间:2024/06/13 05:59
  1. TestViewController * tempVC = [[TestViewController alloc]initWithNibName:@”TestViewController” bundle:nil];

[self.view addSubview:tempVC.view];//存在XIB
调用的是 initWithNibName。

2.
TestViewController * tempVC = [[TestViewController alloc]init];

[self.view addSubview:tempVC.view];//存在XIB
调用的是 initWithNibName & init。

3.
TestViewController * tempVC = [[TestViewController alloc]initWithNibName:@”TestViewController” bundle:nil];

[self.view addSubview:tempVC.view];//不存在XIB
调用的是 崩溃。

4.
TestViewController * tempVC = [[TestViewController alloc]init];

    [self.view addSubview:tempVC.view];//不存在XIB

调用的是 initWithNibName & init。

  1. xib中拖拽其他控件,被拖拽的控件会调用 initWithCoder & awakeFromNib

  2. 代码添加其他控件
    MyView * tempView = [[MyView alloc]init];

[self.view addSubview:tempView];
被拖拽的控件会调用 initWithFrame & init

0 0