user4951
user4951

Reputation: 33050

What Does initWithNibName really do anyway?

theTestController = [[[CustomCell alloc]initWithNibName:@"CustomCell" bundle:[NSBundle mainBundle]]autorelease];

My guess is that it will load CustomCell and set theTestController as the owner. If so:

  1. Why in most sample code for cellForRowAtIndexPath I see [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner: theTestController options:nil]; instead?

  2. What is the difference between [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner: theTestController options:nil]; and theTestController = [[[CustomCell alloc]initWithNibName:@"CustomCell" bundle:[NSBundle mainBundle]]autorelease];

  3. I tried replacing [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner: theTestController options:nil]; with theTestController = [[[CustomCell alloc]initWithNibName:@"CustomCell" bundle:[NSBundle mainBundle]]autorelease]; and I got errors. Looks like the outlets remain nil if I use the latter.

Upvotes: 2

Views: 2645

Answers (1)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

initWithNibName:bundle: is convenience method declared in UIViewController and is available to its subclasses. This will initialize the view controller by loading the nib, probably by using loadNibName:owner:options: method internally.

initWithNibName:bundle: is unavailable to UIView and its subclasses. So we have to make use of loadNibName:owner:options: to load views.

  1. Custom cells are UIView subclasses and hence make use of loadNibName:owner:options:.
  2. Not much difference. initWithNibName:bundle: is a convenience method for UIViewController initialization.
  3. You are getting errors because of the reasons mentioned above.

Upvotes: 6

Related Questions