iOS 控制器view的创建方式(官方文档翻译)
1、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 fileusing 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.
如果控制器已经关联了⼀一个nib文件,这个⽅法就会根据nib文件的描述加载view。
控制器会被关联⼀一个nib文件如果它的nibName属性返回⼀个非 nil 的值。当发生下列其中一种情况 的时候,那么控制器的nibName属性就会关联着⼀一个nib文件。
1) 控制器是从stroyboard实例化的。
2) 使⽤用initWithNibName:bundle:方法初始化控制器时明确的赋值了一个nib⽂文件。
3) 系统根据控制器的名字在应⽤包⾥里找到了关联的nib⽂文件。
如果控制器没有找到关联的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⽅方法中进⾏行。
2、nibName属性官方解析This property contains the value specified at initialization time to the initWithNibName:bundle: method. The value of this
property may be nil.
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:
如果你使⽤nib⽂文件来存储控制器的view,建议你明显的指定nib⽂件来初始化你的控制器。然⽽而 ,如果你不指定nib文件的名字,也没有在你自定义控制器类中重写loadView方法 ,控制器会使用其他方式搜索nib⽂文件,当它的view被访问时候,它会⽤⼀个合适的名 字(去掉.nib后缀名)去查找nib⽂件并加载nib文件(如果找到)。它查找nib⽂文件 使⽤的文件名是下⾯的其中⼀个。
> 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.
如果控制器的类名是以Controller单词结尾的,⽐比如控制器的类名是 MyViewController, 那么系统以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.
如果第一次查找不到nib⽂文件,系统会将控制器的名字作为nib文件的名字去查找是否有对应的 nib⽂文件。
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.
3、控制器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.
1、当控制器view被访问时,如果为空,则控制器会调用loadView去创建view。loadView⽅法默认 的实现做下面两件事:
> 如果控制器有关联的storyboard(也就是说从stroyboard创建),则从 stroyboard中加 载控制器的 view。
> 如果控制器有关联的xib,则从xib中加载控制器的view。
如果控制器没有关联的stroyboard,则会创建一个空的view赋值给控制器的view属性。
<2>、The view controller calls its viewDidLoad method, which enables your subclass to perform any additional load-time tasks.
2、loadView⽅法执⾏行后,控制器会调用viewDidLoad⽅法,使你的子类能够执行任何额外的加载时任务。