-initWithNibName:bundle: - The designated initializer for the UIViewController. Something should eventually call it. However, and despite Apple's examples (which in many cases provide brevity over maintainability), it should never be called from outside the view controller itself.
You will often see this code:
MYViewController *vc = [[MYViewController alloc] initWithNibName:@"Myview" bundle:nil];
I say this is not true. It places the implementation details in the calling program (NIB name and the fact that NIB is used). This violates 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 data back to the object and prevents accidental encapsulation violation of callers. Now, if you change the NIB name or go to the software design, you will fix it in one place (in the view controller), and not in every place where the view controller is used.
Rob Napier Feb 08 '10 at 21:33 2010-02-08 21:33
source share