iPhone View Switching Tutorial
The world of iPhone development can be a bit of a daunting task and irritating at times. I’m the kind of person that likes to do things from scratch as much as I can. So when I headed down this path, I decided to start with the simplest template and create a very, very easy app: creating 2 views and switching between them. After the great lengths (thank you @stevefink) of putting the pieces together and learning, I have decided to write a nice article/tutorial, explaining what I have done to hopefully enlighten or help out anyone going down the same path.
So without further rambling, let’s write some code! (The download is included for your reference, but I recommend following the tutorial first before looking at the provided zip – ed.)
First off lets launch Xcode and get a new project going. We are going to select File > New Project and select “Window-Based Application,” which is one of the most basic app templates you will ever use.
Once you have selected it, click “Choose…” and you will be prompted to enter a name and save it.
Here I have entered the name “NavApp” as my project name, then click “Save.” You will then be presented with your newly created project in the XCode editor/IDE.
Next we are going to setup and add some files that we are going to use. First right click on “Classes” and select “Add Files”.
Then make sure “Cocoa Touch Classes” is selected in the left column, click on “UIViewController subclass” and click “Next”.
Type in the name “RootViewController” (it will append .m) and make sure that “Also create ‘RootViewController.h’” and “Targets: NavApp” is checked.
We are going to do that same process again but we are going to add “ViewTwoController.m” making sure we also create “ViewTwoController.h” and target is selected. Once you are done adding those, your Classes group (the little folder) should look like this:
Now we are going to add to files to Resources (two XIB/Nib files to be exact). Using the same actions mentioned above, create two views, RootViewController.xib and View2.xib.
Then in the left column make sure you select “User Interfaces” and click on “View XIB” after which you will click on “Next” and name it RootViewController.
Repeat the process and add “View2.xib.” Once you do that your project will then look like:
Now we have all the files we are going to need to get rolling. So now let’s setup all of our code in the class files. So click on the “Classes” group/folder and select “NavAppAppDelegate.h.” When you first open the file your code will look like this:
We are going to change that to look like:
Now we are going to open up NavAppAppDelegate.m, which looks like:
And now we are going to change it to look like:
All we have done here is told our applications delegate to setup an instance of a NavigationController and add it to the subview (the short version).
Next we are going to setup our RootViewController, think of this as a kindergarden teacher, that tells which student can move to the front of the line. Now we are going to setup our RootViewController and make it aware of our ViewTwoController and give it an action to handle the switch to that view.
As of right now, our RootViewController.h file looks like this:
We are going to change it to look like this:
As you can see we created an instance variable of ViewTwoController, gave it a property, and also added an IBAction method called ‘switchPage’.
Now that our declarations are in place in our header file (RootViewController.h) let’s open up and add the following code to our RootViewController.m:
Basically what we have done here is created a method in our root controller that will allow for us to switch to our View2(.xib) when a button (tied to switchPage) is clicked. We first check to see if our viewTwoController instances variable has already been created, if not we create a new one and tie it to our View2.xib (we don’t have to add ‘.xib’ to the end because our program is smart enough to already know that) then we set our ‘global’ viewTwoController variable to the newly created instance of ViewTwoController and release it. Once out of our ‘if’ statement we tell our navigation controller to push our new view up to the front; lets animate it so it’s all pretty.
The next to class files in the bunch are “ViewTwoController.h” and “ViewTwoController.m” for this tutorial, these guys need no code.
Now we will move on to setting up our actual interface. We have 3 files here: MainWindow.xib, RootViewController.xib and View2.xib. We want to have our RootViewController.xib to load up first with a button on it that, when clicked, will move us to our second view, or View2.
So double click on MainWindow.xib, this will launch Interface Builder. When that loads we will see:
From here we will want to drag a “Navigation Controller” from our library

and drop it in right under “Window UIWindow”:
Once you have that in place, click the arrow to the left of the Navigation Controller and select “View Controller UIViewController. Once you have done that open up your Identity Inspector (or command+4) and change the class name to RootViewController (which should be listed in your dropdown). Also make sure you make a new reference outlet for your Navigation Controller to your App Delegate.
Once you have done that, then in the Inspector window select “Attributes” or command+1 and set the nib name to: “RootViewController”
Once you do that, your View Layout should change to look like:
Now save and close out of Interface Builder, switch back to XCode and double click RootViewController.xib to relaunch Interface Builder with that file being the focus. (You don’t have to close it out completely, I just do it to give me a clean slate).
On this view, let’s go ahead and drag a button on to the screen, and title it “PRESS ME” or anything to your liking. I also went a head and changed the background color to black:
Now click on “File’s Owner” and then Command+4 (identity) and change it’s class to “RootViewController”
Then control+click drag from File’s Owner, straight down, to View. When the little black box pops up, select “view”.
Now we are going to open up the Connection Inspector for File’s Owner and drag from ‘switchPage’ to our new button right on the view and select Touch Up Inside.
Now click on the View inside your list and look at your connections inspector, if needed (double check, this goofed with me for a minute) drag from “New Reference Outlet” to your “File’s Owner” to insure the connection has been made.
Now save that all up, quit and double click to open View2.xib. Once here we will make a few connections save and run this bad boy!
And there you have it. You can even click the Navigation Item to return to the first screen. This can be expanded any way you want.
Thanks for tuning in and make sure you download the zip in case you have any trouble or questions!
-Take Care,
Wess “Wattz”
Since the days of the Commodore 128, Wess has become an expert in C, C++, Java, Javascript, Python, and PHP and he also has extensive knowledge of Objective-C/Cocoa, Ruby, and Erlang. His blog can be found at www.wattz.net.































Cool, an actual iPhone tutorial! Can’t wait to try this.
Pretty sad to see the “code” all are images instead of selectable text. Would save me a lot of typing.
Wow that is quite a detailed post you have here for iphone view switching tutorial. Thanks I will bookmark this for later!
Marco, you can download the source at the top of the article to save on typing. :) Our SyntaxHighlighter supports C++ but not Objective-C specifically. I will see what we can do moving forward.
I think having it as images is better, you wont take time to learn if you’re just copying and pasting, typing forces you to take it in and read over to make sure you’re not making mistakes.
Will have a go at this later.
Hi, thanks for this great tutorial! I’ve just recently started iphone programming and this was amazing! I have a question about expanding into deeper levels of navigation and how that can be accomplished.
For example, I’m in view 2, I want to be able to have a button that’ll take me to a view 3. Do I have to add a new navigation controller to ViewTwoController and basically repeat the whole tutorial for ViewTwoController.h/.m?
Thanks!
this does not seem to work correctly in 3.0 but it is probably just me….
As Ken asked above, I too would like to know how to add a third view and be able to go to any other two views depending on which button was pressed.
Thanks for the Tutorial it was great but a little rushed at the end. Also a couple of your “To”’s should have been “Two”’s in your descriptions. Not being picky just thought you might want to make the tutorial perfect.
Thanks again,
Nowhereman.
Its nice Man!!!!!!
Starters Cn easily Learn it >D<:
When i try to Build the application using this code i end up with following error:
Request for member ‘navController’ in something not a structure or union.
I am developing under SDK 3.1
Any idea.
Thanks in Advance
I appreciate that anyone would spend their time helping others, but I ended more confused than when I started reading. There are steps missing and the end seemed really rushed.
I’ll will not be able to get this running without downloading the source and going through it line by line, Nib by Nib :/
I also appreciate the time you took but this is a poor example. It shows your true lack of of understanding of objective-c and cocoa development in general. I suggest you go back to basics and read the Apple docs.
Thank you for this super tutorial. Yes there are a couple steps gone but truth is those steps, while aaaggggravating, forced me to learn.
I encountered some frustrating errors, much like “iphoneapp”… i found there was some inconsistency between the .xib files, which are XML files, and the names of variables in my classes. I don’t know why the development environment did not fix them…
…but in any case, i closed out all my windows, opened up the .xib files in a text editor (XML again), and searched for any instances of the outdated variable names, and that fixed the problem(s).
I won’t say that means everything’s A-OK with the dev environment but at least it is something to know!
Nice Tutorial,
How would you go about adding in a third view, and a fourth view and so on, any ones reply – response welcome. Thanks in advanced.
Thanks a lot for the great job, it’s really good tutorial.
I was wondering if adding a second button to navigate you to other page/view would be similar.
I tried using the same code and added another IBAction – switchPage2 which should allows navigation to the new view, but doesn’t seem to do so.
Is there anything I have to do when adding a new view?
Well, I manually went through the tutorial, and just get a white screen. The one line that has me stumped is halfway down, while editing the MainWindow.xib file:
“Also make sure you make a new reference outlet for your Navigation Controller to your App Delegate.”
What does that mean?
First, thanks for the fantastic tutorial!
i really feel like it has gotten me to ALMOST understand how to do this…
Buyt, im also trying to duplicate these steps to add a third view and running into problems. There are a couple of vague statements in the tutorial that might hold the answers…?
“Also make sure you make a new reference outlet for your Navigation Controller to your App Delegate.” – how do you do that?
“double click to open View2.xib. Once here we will make a few connections” – what connections
But specifically, in my new View3.xib file, there is no option when control-clicking on File’s Owner to connect to “View”
Any help would be greatly appreciated!!
Troy wrote:
“But specifically, in my new View3.xib file, there is no option when control-clicking on File’s Owner to connect to “View””
You need to open View3 in Interface Builder and select File’s Owner. The go to the Identity Inspector and select ViewThreeController (or whatever you’ve called it) from the Class dropdown box.
I did this, as well as duplicating all the View2 code for View3, and created a new Button and Action, and it works fine. I now have three views.
Hope this helps.
For those of you unsure of what he meant by some things, “Also make sure you make a new reference outlet for your Navigation Controller to your App Delegate” means control-clicking on the Navigation Controller in MainWindow.xib and linking to ‘Nav App App Delegate’ (or whatever you’ve called your app + App Delegate), and “once here we will make a few connections” means opening the xib for your second view (called View2.xib in the example), selecting ‘File’s Owner’ in the xib window, changing its type/class to ‘UIViewController’ in the Controller Identity Inspector (command + 4) under ‘Class Identity’, and then making a reference to ‘view’ in the Connections Inspector (command + 2) for ‘File’s Owner’.
In summary, the above tutorial is seriously incomplete.
Generally most of the tutor are shoeing only navigating(Switching) from 1st view to 2nd view,But How can we switch from 2nd view to 3 rd view and vice versa
App’s base on Multiple view controllers, and Method for Switching between them(1 to 2 to 3 to 4 and VIce Versa).I have seen your tutor only switching between 1 to 2 and vice versa
I have followed your tutors ..which really help me a lot ……!!!! I want to develop Multiple View with Navigation Based application .
Kindly , please provide a tutor for Multiple Views using Navigation Based Application.
What I mean is suppose if we take 4 Views.(Moving from 1 to 2 to 3 to 4 and VICE VERSA ,using PREVIOUS and FORWARD BUTTON within these views ).
In 1st View,data will be row
(Ex:
DEll
HP
SONY
LENOVA)
and when we click any ROW CONTENT, it must go to 2nd view(which contain some TEXTS, with 2 paragraph with some spacing between 2 paragraphs) .
And in the same way we click on 2nd View it must go to 3rd view ( when we click ,here also TEXT will be displayed and should have a button like BACK(2 nd View) and FORWARD (4 View)BUTTON ).
Generally ,It would be better if we display TEXT in LANDSCAPE MODE .
I will be very thankful if u provide a tutor on it……
WITH A WORD OF THANKS,
ROHITH
Email_id: rohithcse04@gmail.com
Nice tutorial dude….
Thanks for making it simple…
Hey at my system it is not run properly. May anyone
guide me for proper running this application.
Great tutorial but I’m a bit stuck – it compiles but I just get a blank white screen. I think I’ve made all the connections required. Working in SDK 4.01.
Any help greatly appreciated!
You may have not connected Navigation app delegate to your Navigation Controller. Open Navigation App Delegate Inspector Panel (Third Item in MainWindow.xib). Click on + sign in front of navigationController outlet and drag it to Navigation Controller in MainWindow.xib.
I am not able to understand why are we doing this:
“control+click drag from File’s Owner, straight down, to View. When the little black box pops up, select “view”.
Why do we need RootController to connect between RootViewController.xib view with RootViewController.h view when we are not accessing view in XCode that is in RootViewController.h
Does not work for me :-(. No black view with button. I see only the empty white window. I.e. loading from loading from RootViewController.nib does not happen.
Hey… Its a nice code….
but how can add this to a tabbar application?
I think you left something out, or something changed in the new version. Only the window in MainWindow.xib is being loaded, not the Navigation Controller that we added.
For those only seeing a white box. Try this.
Select the App Delegate in MainWindow.xib. Then in connections inspector clickand hold in the circle next to navigationController and drag to NavigationController in the MainWIndow.xib.
Finally It is able to be done
I also added a drill down app into my application but
i still have i problem … its about the detail view.
I Used the Plist to make my drill down navigation and for the detail view but the detail view can’t show.
Can any one help?
This was a great tut. I am having a problem though, My viewTwo wont load when i click the button. The App just crashes. Please Help! Thanks
Thanks for a great tutorial, the code really came in handy, helped me to see how the delegates knitted together.
I used it in XCode4, with little changes needed.
Just to clarify — to get this to work I used the advice of two comments:
1. Alan Zeino:
For those of you unsure of what he meant by some things, “Also make sure you make a new reference outlet for your Navigation Controller to your App Delegate” means control-clicking on the Navigation Controller in MainWindow.xib and linking to ‘Nav App App Delegate’ (or whatever you’ve called your app + App Delegate), and “once here we will make a few connections” means opening the xib for your second view (called View2.xib in the example), selecting ‘File’s Owner’ in the xib window, changing its type/class to ‘UIViewController’ in the Controller Identity Inspector (command + 4) under ‘Class Identity’, and then making a reference to ‘view’ in the Connections Inspector (command + 2) for ‘File’s Owner’.
2. Himanshu
Open Navigation App Delegate Inspector Panel (Third Item in MainWindow.xib). Click on + sign in front of navigationController outlet and drag it to Navigation Controller in MainWindow.xib.
Tried to follow this using Xcode 4.0.1. It’s way too different to be able to follow step by step like the rookie I still am.
Thanks for the tutorial. I’m just learning the language (brand new to C of any kind!) and your example introduced some simplicity to the picture and brought things into perspective for me. I appreciate your having taken the time.
awesome tutorial! got me where I needed to go – thanks!
Hanoah, thanks alot for the comment, that worked for me :-)
You almost had a great tut until you dropped
“Also make sure you make a new reference outlet for your Navigation Controller to your App Delegate.”
What? Is that Greek? I have NO idea what that means and once again stopped following the tutorial and gave up on iPhone programming for a while. ARGH.
Almost.
it keeps telling me that “self.navigationcontroller” in “rootviewcontroller.m” :
“request for member ‘navigationController’ in something not a structure or union”
why?
nevermind. i didnt name my navigationcontroller “navigationcontroller”. i made a short name “navcontr” which did not work at all. appr u cant just name them whatever…
and also. remeber to put everything in a view in the rootviewcontroller.xib and not in a window. this will cause it to not display anything. learned from my mistakes.