50

When should I use init: and when should I use initWithNibName:bundle: when creating a view controller?

nevan king
  • 112,709
  • 45
  • 203
  • 241
Anthony Glyadchenko
  • 3,572
  • 7
  • 48
  • 66

4 Answers4

145

-initWithNibName:bundle: is the designated initializer for UIViewController. Something should eventually call it. That said, and despite Apple's examples (which favor brevity over maintainability in many cases), it should never be called from outside the view controller itself.

You will often see code like this:

MYViewController *vc = [[MYViewController alloc] initWithNibName:@"Myview" bundle:nil];

I say this is incorrect. It puts implementation details (the name of the NIB and the fact that a NIB is even used) into the caller. That breaks encapsulation. The correct way to do this is:

MYViewController *vc = [[MYViewController alloc] init];

Then, in MYViewController:

- (instancetype)init
{
   self = [super initWithNibName:@"Myview" bundle:nil];
   if (self != nil)
   {
       // Further initialization if needed
   }
   return self;
}

- (instancetype)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
    NSAssert(NO, @"Initialize with -init");
    return nil;
}

This moves the key implementation details back into the object, and prevents callers from accidentally breaking encapsulation. Now if you change the name of the NIB, or move to programmatic construction, you fix it in one place (in the view controller) rather than in every place the view controller is used.

Shaheen Ghiassy
  • 7,397
  • 3
  • 40
  • 40
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • 3
    If this is the intended way, why does XCode4 generate a initWithNibName:bundle: stub for every empty ViewController class, yet no init method? –  Aug 16 '11 at 11:48
  • 2
    Same reason that the templates haven't included class prefixes, even though you're definitely supposed to prefix your classes, and the template creates the dumbest possible name for your app delegate, which is a real headache to fix. And IB forces an ivar declaration when you auto-create properties, even though you shouldn't declare ivars in the new ABI. The templates are not always best practice. They're often the least-common-denominator that works somewhat in most cases, but nowhere particularly well. Apple's example code is improving recently, but it still has a way to go. – Rob Napier Aug 16 '11 at 14:40
  • 1
    If you follow the convention of naming your nibs the same as the class then this code works well: `- (id)init { return [super initWithNibName:NSStringFromClass([self class]) bundle:nil]; }` – CharlesA May 16 '14 at 13:13
  • 2
    @CharlesA If you use this approach, you can pass `nil` as the nib name and it'll figure it out based on the view controller class. See the docs for `UIViewController -nibName` that explains the heuristics it uses to find the file. – Rob Napier May 16 '14 at 14:06
  • I've been using this approach for some time now. It's fantastic, thanks Rob! – JVillella Sep 18 '14 at 19:00
  • ditto, this is also my approach. – AW101 Oct 08 '16 at 12:06
9

Use initWithNibName: bundle: if you are... initializing with a nib file! That is, a file that you made using Interface Builder.

If you aren't using IB to layout your views, you can just use init.

Community
  • 1
  • 1
bpapa
  • 21,409
  • 25
  • 99
  • 147
2

You can just call init, as long as the xib has the same name as the view controller class. The encapsulation is not necessary. This saves typing, but may not serve clarity.

 NUDMainViewController *mainVC = [[NUDMainViewController alloc] init];
Kiet Nguyen
  • 609
  • 4
  • 8
1

using init when there is no nib/xib file, e.g. UI are created by coding

using initWithNibName , if we have an nib/xib or same controller share by more than 1 nib/xib

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
 } else {
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
 }

that's what I think..

JosephT
  • 865
  • 1
  • 11
  • 20
  • You know if you add ~iphone or ~ipad to your nib names you can just call initWithNibName@"ViewController" and it will pick the correct one. – HexBlit Feb 12 '14 at 20:45