9

I've been looking at the sample code and sometimes Apple names xib files ClassNameView and sometimes ClassNameViewController. The ClassName is always a UIViewController or UITableViewController, which had me wonder what convention to use when naming a xib. I would think View as it's not really the ViewController, but curious on what the convention is or at least what your naming conventions are for xibs.

4

3 回答 3

3

CocoaTouch 支持这两种约定,首选更短形式的 ClassNameView,根据下面描述的 nib 加载过程,当未指定 nib 名称时(来自UIViewController 文档,在 nibName 下):

具体来说,它会(按顺序)查找具有以下名称之一的 nib 文件:

  1. 如果视图控制器类名以单词“Controller”结尾,如在 MyViewController 中,它会查找名称与类名匹配但不包含单词“Controller”的 nib 文件,如在 MyView.nib 中。

  2. 它查找名称与视图控制器类的名称匹配的 nib 文件。例如,如果类名为 MyViewController,它会查找 MyViewController.nib 文件。

正如其他人所说,这是一个偏好问题!我个人喜欢较短的版本,但可以看到两者的逻辑。

于 2012-07-12T10:39:35.977 回答
2

我使用 ClassNameView 因为 xib 代表视图,而不是视图控制器。我不认为有一个普遍接受的约定。

于 2009-06-06T19:00:56.750 回答
1

我通常使用 ClassNameViewController,因为我将 File's Owner 设置为该类,并且以 nib 表示的对象的子对象命名文件似乎很奇怪。像你一样,我在示例代码中看到了这两种方式,我确实看到了一篇关于 Cocoa 在某些情况下明确寻找 ClassNameView 的博客文章,但我在使用 ClassNameViewController 时没有遇到任何问题。

于 2009-06-06T16:42:29.967 回答