What are the rules for the file name matching initWithNibName?

initWithNibName on iOS is smart: it does things like automatically load different NIBs for iPad instead of iPhone (although many people don’t know this - it is NOT DOCUMENTED in UIViewController.initWithNibName!).

But what else can he do? The only Apple docs I have found for this explain that:

  • It will search and find files with ~ ipad or ~ iphone at the end of the name
  • Apple implies that it complies with the rules for automatic image file name matching

But there is no reference to the rules for matching images, and I cannot find any explicit documents for: (.

Rules may include: "you get another retina NIB if you put @ 2x at the end of the file name" (I have no idea if this works).

What I'm really looking for is a list of what rules are used. This will save a lot of hard coding - repeated in almost every single application that I write - if I could use their smart name matching more (for example, if they have a "boot landscape compared to a boot portrait")

+7
source share
1 answer

To answer your (main) question, these are the rules for matching the file name in initWithNibName :

... However, if you did not specify a nib name and did not override the loadView method in your custom subclass, the view controller controller for the nib file will use other means. In particular, it searches for the tip of the file with the corresponding name (without the .nib extension) and downloads this nib file whenever its presentation is requested. In particular, it looks (in order) for a nib file with one of the following names:

  • If the class name of the view controller ends with the word “Controller”, as in MyViewController, it searches for a nib file whose name matches the class name without the word “Controller”, as in MyView.nib.
  • It searches for a nib file whose name matches the view name of the controller class. For example, if the class name is MyViewController, it looks for the file MyViewController.nib.

From the UIViewController documentation in the nibName section

But there is still ... In fact, the NSBundle have some pretty clever rules for finding resources inside it. Here is the documentation page that describes these rules (see iOS device-specific resources).

What brings us to this page , which describes all the modifiers (and the main template) that can be used to link resources (this I think is the link that you mention regarding the correspondence of the image file name).

+2
source

All Articles