0

Usually when I pass in nil as the first parameter of initWithNibName:bundle: it will automatically find the name, but sometimes it doesn't and it just shows a black screen. Under what circumstances does this happen? There are no errors in the console window and the app happily keeps running as a black screen unless I change the nil to a string literal of the nib name. Where can I check to fix this problem?

borrrden
  • 33,256
  • 8
  • 74
  • 109

3 Answers3

2

You are most likely have a viewcontroller to nib name inconsistancy The role of loading nib are like following

If you pass nil to the nib name the sdk will do the following

  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.

Apple documentation

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • this information comes from [Apple's documentation for the UIViewController nibName property](http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instp/UIViewController/nibName) – Michael Dautermann Jun 07 '12 at 04:46
  • yes @MichaelDautermann am sorry i forgot to include it, ill edit the answer now :) – Omar Abdelhafith Jun 07 '12 at 04:48
1

I've never passed "nil" in the first parameter of [UIViewController initWithNibName: bundle:] before, but Apple's documentation says:

If you specify nil for the nibName parameter and you do not override the loadView method, the view controller searches for a nib file using other means. See nibName.

So presumably you're doing something in your loadView method.

It's probably safest (and a best practice) to probably just specify the explicit .xib name when instantiating your view controller.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • 1
    The thing I like about nil, though, is that if I refactor the class name it will change automatically. Interesting about the loadView section though! That was exactly it. Nice find. – borrrden Jun 07 '12 at 05:13
0

You could always just use init instead and override viewDidLoad.

RileyE
  • 10,874
  • 13
  • 63
  • 106