如何在不将 nib 名称指定为字符串的情况下使用 nib 加载 View Controller 子类?
How to load a View Controller SubClass with a nib without specifying the nib name as a string?
当您以编程方式加载 UIViewController 子类时,如果您将任何文件命名为与您的子类不同的名称,则不会加载与该 ViewController 关联的任何笔尖。
在不向初始化器传递字符串名称的情况下自动加载 nib 的唯一方法是确保您的 x.nib
和 class.swift
与 SubClass
具有相同的名称.
有解决办法吗?
仅当笔尖和 class-file 与 class...
同名时,笔尖才会自动加载
根据文档,您实际上可以加载基于 nib 的视图控制器,而无需将其命名为完全相同的东西。虽然匹配名称是最常用的方法,但还有第二种情况,即使使用 Swift 也应该可以工作。
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:
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.
当您以编程方式加载 UIViewController 子类时,如果您将任何文件命名为与您的子类不同的名称,则不会加载与该 ViewController 关联的任何笔尖。
在不向初始化器传递字符串名称的情况下自动加载 nib 的唯一方法是确保您的 x.nib
和 class.swift
与 SubClass
具有相同的名称.
有解决办法吗?
仅当笔尖和 class-file 与 class...
同名时,笔尖才会自动加载根据文档,您实际上可以加载基于 nib 的视图控制器,而无需将其命名为完全相同的东西。虽然匹配名称是最常用的方法,但还有第二种情况,即使使用 Swift 也应该可以工作。
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:
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.