UIViewController Initializers

When you created a tab bar item for BNRHypnosisViewController, you overrode initWithNibName:bundle:. However, when you initialized the BNRHypnosisViewController instance in BNRAppDelegate.m, you sent it init and still got the tab bar items. This is because initWithNibName:bundle: is the designated initializer of UIViewController. Sending init to a view controller calls initWithNibName:bundle: and passes nil for both arguments.

BNRHypnosisViewController does not use a NIB file to create its view, so the filename parameter is irrelevant. What happens if you send init to a view controller that does use a NIB file? Let’s find out.

In BNRAppDelegate.m, change your code to initialize the BNRReminderViewController with init rather than initWithNibName:bundle:.

B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​h​v​c​ ​=​ ​[​[​B​N​R​H​y​p​n​o​s​i​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​


/​/​ ​T​h​i​s​ ​w​i​l​l​ ​g​e​t​ ​a​ ​p​o​i​n​t​e​r​ ​t​o​ ​a​n​ ​o​b​j​e​c​t​ ​t​h​a​t​ ​r​e​p​r​e​s​e​n​t​s​ ​t​h​e​ ​a​p​p​ ​b​u​n​d​l​e​
N​S​B​u​n​d​l​e​ ​*​a​p​p​B​u​n​d​l​e​ ​=​ ​[​N​S​B​u​n​d​l​e​ ​m​a​i​n​B​u​n​d​l​e​]​;​

/​/​ ​L​o​o​k​ ​i​n​ ​t​h​e​ ​a​p​p​B​u​n​d​l​e​ ​f​o​r​ ​t​h​e​ ​f​i​l​e​ ​B​N​R​R​e​m​i​n​d​e​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​.​x​i​b​
B​N​R​R​e​m​i​n​d​e​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​r​v​c​ ​=​
 ​ ​ ​ ​ ​ ​ ​[​[​B​N​R​R​e​m​i​n​d​e​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​N​i​b​N​a​m​e​:​@​"​B​N​R​R​e​m​i​n​d​e​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​b​u​n​d​l​e​:​a​p​p​B​u​n​d​l​e​]​;​

B​N​R​R​e​m​i​n​d​e​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​r​v​c​ ​=​ ​[​[​B​N​R​R​e​m​i​n​d​e​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

U​I​T​a​b​B​a​r​C​o​n​t​r​o​l​l​e​r​ ​*​t​a​b​B​a​r​C​o​n​t​r​o​l​l​e​r​ ​=​ ​[​[​U​I​T​a​b​B​a​r​C​o​n​t​r​o​l​l​e​r​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

Build and run the application, and it will work just as before. When a view controller is initialized with nil as its NIB name, it searches for a NIB file with the name of the class. Passing nil as the bundle means that the view controller will look in the main application bundle. Thus, BNRReminderViewController will still search for BNRReminderViewController.xib in the main bundle.

This is why we warned you earlier about sticking to the given names when naming files. If you are creating a FidoViewController class that fetches its view from a NIB file, then the only appropriate name for that XIB file is FidoViewController.xib.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset