Very Confused About Loading a NIB file


#1

So I was recently doing chapter 6 and I got a little confused on page 127 when it tells you to load the NIB file. It asks you to use the following code.

[code]NSBundle *appBunle = [NSBundle mainBundle];

// Look in the appBundle for the file BNRReminderViewController.xib
BNRReminderViewController *rvc = [[BNRReminderViewController alloc]initWithNibName:@“BNRReminderViewController” bundle];
[/code]

The reason I am confused is because I have no idea why we even have to load the NIB file. If I connect the .XIB File to the File’s Owner, set the view outlet, and make the connections like it asks you to on pages 128 - 130, and change the code to the following, it still works perfectly fine. The project compiles and runs without any errors

BNRReminderViewController *rvc = [[BNRReminderViewController alloc]init];

Could someone please help me understand this? Thanks in advance


#2

The init method of the UIViewController does its best to complete the intialisation process.

First, it looks for a suitable XIB file in the main bundle and loads the file if it finds one. If an XIB file with the same name as your custom view controller’s name exists, it will load that file.

The example in the book is demonstrating that you can create a view controller from an XIB file in a bundle which you specify.

[Become a competent programmer faster than you can imagine: pretty-function.org]