Name conventions for xibs

大城市里の小女人 提交于 2019-12-03 17:45:40

问题


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.


回答1:


I use ClassNameView since the xib represents the view, not the view controller. I don't think there is a generally accepted convention.




回答2:


CocoaTouch supports both conventions, with a preference for the shorter form ClassNameView, as per the nib loading process described below when a nib name is not specified (from the UIViewController documentation, under nibName):

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.

So as other have said, it's a matter of preference! I personally like the shorter version but can see the logic in both.




回答3:


I generally use ClassNameViewController since I set the File's Owner to that class and it seems strange to name the file after something that's a sub-object of the object the nib represents. Like you, I have seen it both ways in sample code and I did see a blog post about Cocoa explicitly looking for ClassNameView in some cases, but I haven't had any problems using ClassNameViewController.



来源:https://stackoverflow.com/questions/960042/name-conventions-for-xibs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!