views:

601

answers:

3

I'm coding in Objective-C for the iPhone. I was wondering when I use init: and when I use initWithNibName:bundle: when creating a view controller.

I can't really find an answer to this question.

Thanks.

+1  A: 

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

bpapa
+1  A: 

If you plan on creating your view with Interface Builder then you would use initWithNibName:bundle:. The init function is used if you're going to setup your view hierarchy in code.

A lot of people only use Interface Builder to construct apps, which gets the job done, but I would recommend learning how to do it all in code. IB automates a lot of things and the end result is not always exactly what you want, but if you set everything up in code you have much more control. I've used interface builder very little, and I'm glad I've taken the time to learn how to do lay it all out in code. If you're really in a time crunch IB might be the quicker way, but in the long run manually programming views is worth it.

Charybdis
I agree about knowing how it could be done in code, but disagree that IB will hurt you in the long term. It's very nice (and more MVC separated) to have your views laid out and merely referenced in your controller. Plus it makes it very simple to do something like iPad support where the screen is a different size since you simply load a different nib file designed for a bigger screen, but otherwise have the same underlying code. And you can still "fix" views in code in ways IB can't, but there is great value not hardcoding pixel values form your controller.
Squeegy
Good Point! Thanks
Jordan
@Squeegy, agreed. IB is a core part of Mac and iPhone development going all the way back to NeXTSTEP (thus the "N" in NIB). It is not a crutch; it's a key tool and well worth learning and understanding.
Rob Napier
@Squeegy, You can have your view fit to screen size with initWithFrame: [[UIScreen mainScreen] bounds]].I didn't mean to say that IB is /bad/, I'm just saying I think it's nice to know how to do it all in code.
Charybdis
@Charybdis, Autoresize masks help to, but it's likely only going to get you half way. I just humbly disagreed with the line: *but in the long run manually programming views is worth it.*
Squeegy
+3  A: 

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

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

- (id)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.

Rob Napier