UIViewController

来源:互联网 发布:知乎的收藏夹在哪 编辑:程序博客网 时间:2024/05/16 13:59

UIViewController的创建方式

代码创建

  // 1.创建 window    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    self.window.backgroundColor = [UIColor whiteColor];  // 2.代码创建控制器    ViewController *vc = [[ViewController alloc] init];    vc.view.backgroundColor = [UIColor orangeColor];  // 3.设置控制器为窗口根控制器    self.window.rootViewController = vc;  // 4.显示窗口    [self.window makeKeyAndVisible];

storyboard创建

方式一:

    // 1.主动加载storyboard(仅仅是将stroyboard文件加载到内存中,并没有创建里面的控制器)    UIStoryboard *stroyboard = [UIStoryboard storyboardWithName:@"Stroyboard" bundle:nil];    // 2.创建初始化控制器(箭头指向的控制器)    UIViewController *vc = [stroyboard instantiateInitialViewController];

方式二:

    // 1.主动加载storyboard(仅仅是将stroyboard文件加载到内存中,并没有创建里面的控制器)    UIStoryboard *stroyboard = [UIStoryboard storyboardWithName:@"Stroyboard" bundle:nil];    // 2.根据stroyboardId 创建对应的控制器    UIViewController *vc = [stroyboard instantiateViewControllerWithIdentifier:@"redController"];

stroyboard使用注意点:

  • 如果项目中有多个stroyboard,应用启动只会加载最主要的stroyboard。其他 stroyboard必须主动写代码去加载。

  • 如果没有设置最主要的stroyboard,则必须手动创建window和控制器,并且设置window的根控制器。想要加载非主要stroyboard就必须主动写代码去加载,并主动创建stroyboard里面的控制器。

  • 有设置最主要stroyboard项目其内部本质上也是通过下面三步来启动项目的: 
    1> 创建window 
    2> 加载最主要storyboard,并创建初始化控制器(箭头指向的控制器) 
    3> 设置window的根控制器,并显示窗口。

通过xib创建控制器


CZViewController *vc = [[CZViewController alloc] initWithNibName:@"View34234" bundle:nil]; 

xib的作用

    用来描述控制器的view长什么样子。或者说控制器会根据传人的xib文件来创建自己的view。

注意点

    xib只能拖view进去,实际开发中千万不要拖控制器到xib中去,否则会出现莫名其妙的错误。

错误剖析

    1> -[UIViewController _loadViewFromNibNamed:bundle:] was unable to load a nib named "View34234"'    错误原因:xib里面没有任何内容。    2> -[UIViewController _loadViewFromNibNamed:bundle:] loaded the "View34234" nib but the view outlet was not set.'    错误原因:xib里面的view没有设置为控制器的view。因为xib中可能有多个view。所以要告诉控制器去加载属于它的view。

Files Owner顾名思义,就是这个nib文件的拥有者,由拥有者来负责load这个nib。

xib和stroyboard创建控制器的好处

  • 可以直接拖控件
  • 可以省下大量代码和时间,从而得到更快的开发速度

关于loadView方法调用的官方说明

You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property.

你不应该直接调用这个方法,当需要访问控制器的view但当前它的view为nil时,控制器会调用这个方法加载或创建一个 view 并且赋值给它的view属性。

If the view controller has an associated nib file, this method loads the view from the nib file. A view controller has an associated nib file if the nibName property returns a non-nil value, which occurs if the view controller was instantiated from a storyboard, if you explicitly assigned it a nib file using the initWithNibName:bundle: method, or if iOS finds a nib file in the app bundle with a name based on the view controller€™ class name. If the view controller does not have an associated nib file, this method creates a plain UIView object instead.

   如果控制器已经关联了一个xib文件,这个方法就会根据xib文件的描述加载view。一个视图控制器如果它的nibName属性返回一个非空的值,那么它就关联了一个nib文件。如果控制器是从stroyboard实例化,或者如果你使用initWithNibName:bundle:方法明显的赋值了一个xib文件,或者如果系统根据控制器的名字在应用包里找到了关联的xib文件。控制器的nibName属性就会关联着一个xib文件。如果控制器没有找到关联的xib文件,这个方法会创建一个普通的view对象并赋值给控制器的view属性。

If you use Interface Builder to create your views and initialize the view controller, you must not override this method.

 如果你使用 xib创建 view 并且初始化控制器,你必须不能重写这个方法

You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view property. The views you create should be unique instances and should not be shared with any other view controller object. Your custom implementation of this method should not call super.

为了能手动创建你的 views,你可以重写这个方法。如果你选择这么做,将根视图赋值给控制器的 view 属性。你创建的视图应该是独一无二的,不应该跟其他控制器对象共享。你在自定义这个方法的实现时不应该调用父类的该方法。

If you want to perform any additional initialization of your views, do so in the viewDidLoad method.

   如果你想对你的 views执行任何额外的初始化操作,可以在 viewDidLoad 方法中进行。

NibName属性官方解析

This property contains the value specified at initialization time to the initWithNibName:bundle: method. The value of this property may be nil.

    这个属性包含指定的值在使用initWithNibName:bundle:方法初始化时候。这个属性的值可能为零。

If you use a nib file to store your view controller’€™s view, it is recommended that you specify that nib file explicitly when initializing your view controller. However, if you do not specify a nib name, and do not override the loadView method in your custom subclass, the view controller searches for a nib file using other means. Specifically, it looks for a nib file with an appropriate name (without the .nib extension) and loads that nib file whenever its view is requested. Specifically, it looks (in order) for a nib file with one of the following names:

   如果你使用xib文件来存储控制器的view,建议你明显的指定xib文件来初始化你的控制器。然而,如果你不指定xib文件的名字,也没有在你自定义控制器类中重写loadView方法,控制器会使用其他方式搜索xib文件,它会用一个合适的名字(去掉.xib后缀名)去查找xib文件并加载xib文件(如果找到)当它的view被访问时候。它查找xib文件使用的文件名是下面的其中一个。

If the view controller class name ends with the word ‘Controller’, as in MyViewController, it looks for a nib file whose name matches the class name without the word ‘€œController’, as in MyView.nib.

It looks for a nib file whose name matches the name of the view controller class. For example, if the class name is MyViewController, it looks for a MyViewController.nib file.

NOTE

     Nib names that include a platform-specific identifier such as ~iphone or ~ipad are loaded only on a device of the corresponding type. For example, a nib name of MyViewController~ipad.nib is loaded only on iPad. If your app supports both platform types, you must provide versions of your nib files for each platform.

控制器View的加载过程

官方描述如下:

    1、The view controller calls its loadView method. The default implementation of the loadView method does one of two things:        > If the view controller is associated with a storyboard, it loads the views from the storyboard.        > If the view controller is not associated with a storyboard, an empty UIView object is created and assigned to the view property.   2、The view controller calls its viewDidLoad method, which enables your subclass to perform any additional load-time tasks.

翻译如下:

    1、当控制器view被访问时,如果为空,则控制器会调用loadView去创建view。loadView方法默认的实现做下面两件事:        > 如果控制器有关联的storyboard(也就是说从stroyboard创建),则从 stroyboard中加载控制器的 view。        //> 如果控制器有关联的xib,则从xib中加载控制器的view。        > 如果控制器没有关联的 stroyboard,则会创建一个空的 view赋值给控制器的 view 属性。   2、loadView方法执行后,控制器会调用viewDidLoad方法,使你的子类能够执行任何额外的加载时任务。

这里写图片描述

0 0
原创粉丝点击