Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the rules for filename matching initWithNibName?

initWithNibName on iOS is clever: it does things like automatically load different NIB for iPad instead of iPhone (although lots of people don't know this - it IS NOT DOCUMENTED in UIViewController.initWithNibName!).

But what else can it do? The only Apple docs I've found for this explain that:

  1. It will look for and find files with ~ipad or ~iphone on the end of the name
  2. Apple implies that it follows the rules for automatic image filename matching

But there's no link to the rules for image matching, and I can't find any explicit docs for either :(.

Rules might include: "you get a different NIB for retina if you put @2x on the end of the filename" (I have no idea if this works).

What I'm really looking for is a list of what rules ARE used. It would save a lot of hardcoding - repeated in almost every single app I write - if I could make more use of their intelligent name matching (e.g. if they have a "load landscape nib vs. load portrait nib")

like image 834
Adam Avatar asked May 07 '12 13:05

Adam


1 Answers

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

...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.

From UIViewController's documentation under nibName

But there is more... Actually the NSBundle have some pretty smart rules for locating resources inside it. Here is the documentation page describing these rules (see: Device-Specific Resources in iOS).

Which brings us to this page that describes all the modifiers (and the basic pattern) that can be applied to bundle resources (this I believe is the link you're mentioning regarding image filename matching).

like image 115
Alladinian Avatar answered Oct 16 '22 15:10

Alladinian