In this tutorial, I am going to briefly show you how to add multiple NIB files to your iPhone app. This is a good beginner tutorial for using different views.
If you have any questions about this tutorial, please comment below. Ok, let’s go ahead and dig in.
1) Open up XCode > New Project > View Based Application
2) Call it Alarm Clock App and hit Save
If you look under your resources folder, you can see XCode already gives you new NIB (.xib) files, and already interconnects them
3) Now we need to add another NIB file, but first lets add the class files first. Click File > New File UIViewController subclass. Call it ExtraViewController.m and hit Save.
4) It will be added under your classes folder. Now, under your Resources folder, double click on MainWindow.xib to open up Interface Builder. Click File > New > Empty and then click Choose.
5) Then click Tools > Library to open up your library
6) Drag in View from your library to your new window box from the File you just created.
7) Select File’s Owner, then go to Tools > Inspector, and then go to the “i” tab.
8 ) Type in ExtraViewController as the Class name, save it and save Interface Builder. Make sure you save it in the designated App Folder. And call it whatever NIB file you would like. I saved mine as ExtraNIB.
9) A box will pop up, make sure you check the box of your app name and click Add. Now you will see that ExtraNib.xib was added to your project. Drag it in the Resources folder for organizational purposes.
10) Go back to Interface Builder, and in your ExtraNib file, and with the Identity window open, click on the Connections Tab (it’s the arrow). You should see multiple Outlets. If not, you have done something wrong.
11) You should see View as one of the outlets. Now click on the circle next to View and drag it to View in the ExtraNib file window. Hit Command + S to save.
12) Now let’s add a Label to the View. Double Click View from the window box. Click on Tools > Library and drag Label onto the View window. Change the Label text to “THIS IS NIB VIEW 2″ and resize as you’d like. Save.
13) Return to XCode. In ExtraViewController.m – you need to uncomment the group of code. I.E – “The designated initializer. Override to…” See image below:
14) Go to AlarmClockAppAppDelegate.h and add @class ExtraViewController; right underneath the other class. We also need to declare it. To do this add ExtraViewController *extraViewController; as the last line in the @interface group of code. Make sure its before the }
15) Go to AlarmClockAppAppDelegate.m and add extraViewController = [[ExtraViewController alloc] initWitNibName:@"ExtraNib" bundle:[NSBundle mainBundle]]; after the //Override point for customization after app launch line
16) Comment out [window addSubview:viewController.
17) Still in AlarmClockAppAppDelegate.m, add another #import line as #import "ExtraViewController.h" to import that class
18) Add [window addSubview:extraViewController.view]; under what you just commented out (the next line break)
19) Save it and click Build and Go.







Visit Jonathan Crowe's Website
Visit Jonathan Crowe on Twitter










Subscribe to Posts

6 Responses
thanks jonathan, this piece of information was truly useful for me as i am just into iphone app development. thank you once again. Have a nice day
@Vaishnavi – My pleasure. Let me know if you have any specific requests for a tutorial!
Hi Jonathan,
Is it possible to have Extra View Controller inside a View Controller and not in the application delegate like the one you have? I am developing a program where I am planning to use multiple nib files. I have a main page loaded from a nib. It has a sidebar which consists of an array of buttons (which do not change) and contents section in which corresponding nibs are displayed (based on which button is pressed). I am new to iPhone development and your help would be very much appreciated. Thanks.
Hi Again!
I was browsing the net and I found this site.
http://stackoverflow.com/questions/471308/on-os-x-seperating-a-gui-into-multiple-nib-files-and-nsview-subclasses
His solution is pretty much the same. The only thing is that I am confused how _accessoryContentViewController becomes connected and displayed to the main view.
This tutorial has been very useful especially as am new into iPhone Development. Thanks for putting this up.
@clyde I believe I can help you out..check out my new tutorial on the homepage
its called switching view controllers method 1
it should help you!