1

I have noticed that when adding a viewController with interface builder and a nib,

That I don't have to call initWithNibName for it to pick up the associated nib, I can just call init!

Any idea why?

ie.

This:

NotificationManagementController *notificationView = [[NotificationManagementController alloc] initWithNibName:@"NotificationManagementController" bundle:nil andCurrentNotifications:nil];

and This:

NotificationManagementController *notificationView = [[NotificationManagementController alloc] init];

Both seem interchangeable....

Thus if I then call these line of code:

notificationView.delegate = self;
notificationView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:notificationView animated:YES completion:NULL];

I see all changes in the nib.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • 2
    Take a look at the [documentation](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instp/UIViewController/nibName): "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. (...)" – albertamg Aug 04 '13 at 18:31

1 Answers1

2
NotificationManagementController *notificationView = [[NotificationManagementController alloc] initWithNibName:@"NotificationManagementController" bundle:nil andCurrentNotifications:nil];

is simply not neccessary, and even frowned upon by some (including me).

NotificationManagementController *notificationView = [[NotificationManagementController alloc] init];

is much cleaner (and safer) in that is hides implementation details, but will effectively call initWithNibName: behind the scenes.

I like to think of it this way:

- (id)init 
{
    self = [[NotificationManagementController alloc] initWithNibName:@"NotificationManagementController" bundle:nil andCurrentNotifications:nil];
    if (self)
    {
        // Initialization
    }
    return self;
}
Cole
  • 93
  • 7
  • sounds great! but what If I want to add further input parameters to my init method, initWithNibName is the only one exposed? So I was extending that, why is it bad to call withNibName? – Woodstock Aug 04 '13 at 18:29
  • 2
    [Because it breaks encapsulation](http://oleb.net/blog/2012/01/initWithNibName-bundle-breaks-encapsulation/) – albertamg Aug 04 '13 at 18:33
  • 2
    @JohnWoods Extra input parameters can be added by adding custom `init`methods to your VC. Don't get me wrong lots of developers use `withNibName`, I just feel that it's better to use it once in another `init` method. If you were to change your Nib name for instance, having `withNibName` only called once makes refactoring a bit cleaner :) – Cole Aug 04 '13 at 18:35
  • Many thanks for that 'it break encapsulation' link, @albertamg! – Alex Dec 01 '15 at 12:24
  • By the way, if you do that in Xcode 7 (what the link with 'it break encapsulation' says), you will get 2 warnigs. Use this to silence the warnings http://stackoverflow.com/questions/25429685/turn-off-designated-initializer-checking-in-xcode-6 – Alex Dec 01 '15 at 12:39
  • @Alex you are welcome! Btw, if you mark your init method as the new designated initializer, those warnings should go away: `- (instancetype)init NS_DESIGNATED_INITIALIZER;` (you will need to override `initWithCoder:` as well) – albertamg Dec 01 '15 at 14:37