UIKit 使用什么模式来确定要加载的正确笔尖名称?

发布于 2025-01-01 05:25:30 字数 867 浏览 1 评论 0原文

我有一个奇怪的问题: 当您没有显式指定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽手叙旧 2025-01-08 05:25:30

来自 UIViewController.nibName 属性的文档:

如果您使用 nib 文件来存储视图控制器的视图,那么它是
建议您在以下情况下明确指定该 nib 文件
初始化你的视图控制器。但是,如果您不指定
nib 名称,并且不要覆盖自定义中的 loadView 方法
子类,视图控制器使用其他搜索 nib 文件
方法。具体来说,它会查找具有适当名称的 nib 文件
(不带 .nib 扩展名)并在每次查看时加载该 nib 文件
被要求。具体来说,它(按顺序)查找带有以下内容的 nib 文件:
以下名称之一:

  1. 如果视图控制器类名以单词“Controller”结尾,如下所示
    在 MyViewController 中,它会查找名称与
    不带“Controller”一词的类名,如 MyView.nib 中。
  2. 看起来
    对于名称与视图控制器名称匹配的 nib 文件
    班级。例如,如果类名称是 MyViewController,则它看起来
    对于 MyViewController.nib 文件。

From the documentation of UIViewController.nibName property:

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:

  1. 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.
  2. 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文