UIKit 使用什么模式来确定要加载的正确笔尖名称?
我有一个奇怪的问题: 当您没有显式指定 nibName
时,UIKit
使用什么模式来确定 UIViewController
的正确 nib 名称?
我为什么要问?
因为我认为(有充分的理由)系统将首先查找与控制器类同名的笔尖。 也就是说,如果我有一个VideoViewController
,系统将首先查找VideoViewController.xib
。
显然情况并非如此。 我有另一个类,VideoView
,它是UIView
的子类。我有一个名为 VideoView.xib
的笔尖,其中包含 VideoView
的所有视图层次结构。
当我尝试运行该应用程序时,它立即崩溃并出现 setValue:ForUndefinedKey
异常。 我注意到该键始终是来自 VideoView.xib 的键,而不是来自 VideoViewController.xib
的键。这意味着系统正在尝试为名为 VideoViewController
的视图控制器加载 VideoView.xib
。
看起来,如果系统找到一个名称与控制器类名类似的笔尖(仅没有“Controller”后缀),它将使用该笔尖。
我通过将我的类重命名为 VideosViewController (带有“s”)来确认这一点,并且一切正常。
还有像这样的边缘条件吗? 或者有没有地方指定具体的算法?
我花了一个小时来调试这个,我不想将来在同样的问题上浪费更多时间......
谢谢!
I have a weird question:
What is the pattern that the UIKit
is using to figure out the right nib name for a UIViewController
, when you don't explicitly specify the nibName
?
Why am I asking?
Because I thought (With a good reason) that the system will first look for the nib with the same name as the controller's class's.
That is, if I have a VideoViewController
, the system will first look for VideoViewController.xib
.
Apparently this is not the case.
I have another class, VideoView
, which is a subclass of UIView
. I have a nib called VideoView.xib
that contains all the view hierarchy for VideoView
.
When I try to run the app, it crashes immediately with setValue:ForUndefinedKey
Exception.
I noticed that the key is always a key from the VideoView.xib, and not from VideoViewController.xib
. That means that the system is trying to load VideoView.xib
for a view controller named VideoViewController
.
It seems that if the system will find a nib with a name similar the controller class name -only without the "Controller" suffix, it'll use that nib.
I confirmed it by renaming my class to VideosViewController
(With 's'), and everything worked just fine.
Are there any more edge conditions like this one?
Or is there a place that specify the exact algorithm?
It took me an hour to debug this one, and I don't want to waste any more time in the future on the same problem...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 UIViewController.nibName 属性的文档:
From the documentation of UIViewController.nibName property: