iPhone Programming Tutorial – Transitioning Between Views

August 3rd, 2008 Posted by: brandontreb - posted under:Tutorials

This tutorial will focus on transitioning from one view to another. We will be utilizing Apple’s UINavigationController. I will be using the code from the “Hello World” tutorial that I previously wrote. So if you have not completed it yet, go ahead and do it and come back to this page. (It’s quick I promise). You can view it here.

In this tutorial you will learn:

The first thing we are going to do is change our “Hello World” text to say something that sounds more like navigation. Go ahead and open RootViewController.m. Location the cellForRowAtIndexPath method (it’s the one that you edited to display “Hello World” in the table cell.

Change the line: [cell setText:@"Hello World"] ; to [cell setText:@"Next View"];

Add A New View

We will now add the view that we will be transitioning to. Click on RootViewController.xib and this should open up Interface Builder. We don’t actually need to edit this file. Once inside Interface Builder click on File -> New and select View.

It will add a blank View to your project. For now, we will keep it simple. Go ahead and drag a new Label on to the View. Double click on the label and change the text to whatever you want. I set mine to View 2 (I know, not very imaginative).

Let’s save the view. Click File -> Save. Call it View2. Make sure that you save it inside your Hello World project’s directory. It may want to save it somewhere else by default.

Next, you will see a screen asking you if you want to add the View to your project. Check the box next to Hello World and click Add.

Close Interface Builder. Drag the View2.xib file into the Resources folder, if it didn’t appear there by default (this will help maintain organization).

Add A View Controller

Now we need to create a ViewController class. This class will be used to connect the view that we just created to our code. Inside of Xcode click File -> New File… Select UIViewController subclass and click Next.

Name it View2ViewController and make sure “Also create “View2ViewController.h” “ is checked. Click Finish to continue. This will add the new ViewController to your project.

For organization sake, drag your View2ViewController.h and .m files into the Classes folder if they didn’t appear there to begin with.

Set Up The Transition To The New View

Open up RootViewController.h and add the following code:

This code should be pretty self explanatory, but I will explain it anyway. The import statement #import “View2ViewController.h” gets the header file of the ViewController that we created and allows us to create new instances of it.

Next, we declare a variable called view2ViewController which is of the type View2ViewController. One thing that I want to point out is the first part starts with a capitol “V” which is the type of object that we are declaring. The second part starts with a lower case “v“. This is our variable name that we will use to reference our ViewController object. Finally, we make our variable a property to set additional information.

Now, open up RootViewController.m and add the following code underneath @implementation RootViewController. This creates default “getter” and “setter” methods for our view2ViewController property.

Next find the function didSelectRowAtIndexPath. It may be commented out. If it is, go ahead and uncomment it. This method gets called (automatically) every time a table cell gets tapped. Notice that it takes an indexPath. This will be useful later on when I show you how to populate a UITableView with an NSArray. We will ignore it for now.

Add the following code to the method.

First we check to see if self.view2ViewController is null. This will happen the first time the user presses on the table cell. After this, the viewController gets stored in memory to optimize performance. Next we create a new instance of a View2ViewController and set it to our view2ViewController. Remember to pay attention to a capitol “V” verses a lowercase “v”. After we set this viewController to our viewController, it should be released. Remember, objective-c does not have a garbage collector, so we need to clear all of our unused objects from memory.

Finally, the last line of code is what actually transitions our view to the newly created view. The navigationController object is a stack that contains viewControllers. The view at the top of the stack is the one that gets rendered. So all we are doing is pushing a viewController onto this stack. There last part animated:YES, tells the compiler that we want an animated transition to the next view.

Connect The View To Code

Before this code will execute, we must connect the code that we just wrote to the view we just created. Double click on View2.xib to open it up in Interface Builder. We need to associate our View2ViewController class object with the view so click on the File’s Owner object and then click Tools -> Identity Inspector.

Click the dropdown next to class and select View2ViewController.

Next click anywhere on your view to select it. Click Tools -> Connections Inspector. Click in the circle next to New Referencing Outlet, drag it to the File’s Owner object and release it. The word view will popup. Go ahead and click on it.

Close Interface Builder. You can now click Build and Go. When you click on the words “Next View”, you will see the screen transition to your new view. There is still one thing missing. There is no back button in the NavigationController at the top. Apple actually adds this for us, but we need to set a title on our main view.

Adding The Back Button

Close the iPhone Simulator and open RootViewController.m. In the viewDidLoad method (gets called when the view is first loaded) add the following code.

Since RootViewController extends Apple’s class UITableViewController, it comes with a title property. You can set this to anything you want. I have set it to the string “Hello”. Now click Build and Go and you are done. Here are a few screenshots.

When you click on “Next View” it should transition to:

Notice the back button at the top with the text “Hello”. If you press it, your view will be popped from the NavigationController stack and the previous view will be shown. If you have any problems/questions/comments post them in the comments. I’m pretty good about answering them as it emails me when you do so and I receive them on my iPhone.  If you have any problems, you can download the source code here Hello World Views Source.  Happy iCoding!

  • Ricky

    Great tutorials. I was trying to find a contact, but I couldn’t so I thought I would post here instead. I was wondering if you could explain the difference between the different Project types. Like when would you use a view based application, a window based application. Why wouldn’t you just create a window based application and drag out a View or drag out a Navigation View instead of creating a Navigation Based application. Thanks.

  • http://icodeblog.com Brandon


    Thanks for commenting. Each project type is essentially a template. They are in place to make your job easier. When you create an iPhone application, it will generally fit into one of those categories so Apple has helped us out and written some of the code that would generally be written over and over again.

    So, you would use each project type depending on the project. For example, if you were creating a to-do list, you might want to start with a Navigation-Based application as it comes with a UITableView built in. However, if you want to create an application with many UI controls (maybe a timer), you would want to start with a Window-Based application so that you start with a blank view.

    The only reason I can think of to not create a Window-Based application is laziness/efficiency. Why write code that has already been written for you? I hope I have answered your question. Thanks for reading.

  • Brian

    Great tutorial as always and thank you for sharing! I’d love to see a tutorial on handling XML data and displaying it in a UITable along with say an image thumbnail, etc. Haven’t found a tutorial like that anywhere! Cheers and keep up the great work!

  • Tyten


    Great work on the tutorials, it’s a great help for me …
    Just one question: is it possible to add the source code of this specific tutorial? Because I’ve done everything like you say in the tutorial but for some reason it’s giving like 12 errors :o
    Thanks in advance!

  • @Tyten

    Check your code in your .h and .m files making sure that the cases are correct. The first go around I had at this I capitalized a property that should have been lowercase. Otherwise, just make sure you didn’t skip a step. I tested it does work as advertised.

  • Tyten

    ok, I’ve tried again and … it works! I have no idea what I did wrong but now it works and all makes sense, thanks!

  • http://icodeblog.com Brandon


    Glad to see you got it working! I went ahead and included the source code anyway.

  • Adam

    Thanks Brandon. Again u r a life saver. There is a lack of examples in iPhone documentation. I’m struggling to understand some of the programming guide and references apple put up.

    By the way declaring an extra variables to store the view:
    if ( self.view2ViewController == nil ) {
    self.view2ViewController = [[View2ViewController alloc] initWithNibName:@”View2″ bundle:[NSBundle mainBundle]];
    self.view2ViewController = view2;
    [view2 release];

    why not do a direct assignment to remove the extra codes:

    if ( self.view2ViewController == nil ) {
    self.view2ViewController = [[View2ViewController alloc] initWithNibName:@”View2″ bundle:[NSBundle mainBundle]];

    Okay I know this may be out of scope but have you got any idea how to populate the photo album in simulator with photos?

  • Tyten

    Probably a stupid question but … how do you add in more rows/cells linked to other views?
    I Can’t really find it, Thanks again!

  • http://icodeblog.com Brandon

    I was just following Apple’s convention. I understand that it might be quicker not to build an entirely new object. I guess the reason it might be beneficial is if you want to check if the new object is null before you assign it to your classes object. Good catch.

    My next tutorial will detail how to populate a UITableView with an array. I will show you how to display something different based on the row you clicked on. Chances are however, you would not want to display an entirely different view depending on the cell clicked. If this is the functionality you are after, I would suggest using a different control. If my next tutorial doesn’t solve your problem, comment on it and I’ll see if I can help you from there.

  • Tyten


    I think your next tutorial is exactly what I want! :D
    Keep up the great work! you’re helping a lot of people …

  • http://www.digg.com Neb

    I agree with Tyten, I would love some help with populating lists and then having different views for each item selected. If you can do that it would be fantastic! your tutorials have really helped so far.

  • http://Onetimetransition? Bob Schoenburg

    Fabulous tutorial on view transitioning. Thanks!

    Is this “one time” run? When I click Next View, it goes there, and the Hello (back button) works, but the next time I click Next View, nothing happens???

  • Bob Schoenburg

    Found my error! One line of code was out of place. Works great now!

  • Pulkit


    This a wonderful tutorial and I learned a lot from it.

    But I have question, I want to create an application which will first ask user’s login info and then it will take the user to the selection screen where he can select the game of his choice and then based on the game selection the next screen will show up.

    So, my question is when you select a “Navigation-based” application in X-Code it will by default take you to the “Table-View” page. But I want to have login screen before that and then I will show the Table View.

    What should I do here?


  • Uhu

    thanks for the good tutorial. I have some questions about it though:

    What does the @property (nonatmic, retain) statemant do exactly?


    in the didSelectRowAtIndexPath method:

    I do not understand, what the connection between the View2ViewController class and the self. statement is. Why do I have to call View2ViewController with self.View2ViewController?

  • Adam

    @property (nonatmic, retain) is a property declaration for the class. Much like how u define property for a car e.g wheels, windows, handbrake.

    @property is a directives to tell the the compiler that whatever follows would be the property.(nonatmic, retain) are the attributes of the property that specific how the property should be handled.

    As for View2ViewController, RootViewController has created an instances of View2ViewController class in it. For a class to reference it own instances, we normally include “self” to instruct the compiler that the class itself is calling the method.

    It is consider a good practise to declare all variables explicitly so that other programmers using your code can understand it. You can remove the “self” in the codes above and it would still work. This is because the compiler knows that you are referring to a property in itself.

    Feel free to correct me if I’m wrong. I’m learning too. :)

  • Uhu

    Aah thank you. Is there a docu for the property thing?

    I do not exactly know, why this @property has to be declared.

  • Pulkit

    Can somebody comment on my post as well?:-)

  • Adam

    I guess you can start with a View-Based Application template. Then create a new view and Subclass “UITableViewController”.

  • http://icodeblog.com Brandon


    Your question is a little out of the scope of this discussion, however I will try to point you in the right direction.

    The “Navigation Based” template is meant for applications that start with a UITableView. If you want a different view to show by default, you will have to create it in Interface Builder, then create a ViewController for it.

    To load your new view by default you will have to alter your AppDelegate.m file to load the new view you created instead of the default view.

    Your best bet is to do like Adam says and start from the ground up. I’m sorry that I can’t be more specific as this is a corner case and it would take a lot for me to write you a full tutorial on it.

    I hope I helped a little…

  • http://icodeblog.com Brandon


    has a good explanation of properties. It might be useful to you.

    I haven’t found any formal documentation on properties.

  • Pulkit

    Thanks for the help Brandon and Adams can you point some links which talks about “Navigation Controller” in detail?

  • Uhu

    so I tried the following to get the thing to move on to different Views.

    I created View1.xib, View2.xib and View3.xib

    In Method UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    I iterate the int variable i, which is declared in RootViewController.h to give me the table cell output like:
    Cell 1
    Cell 2
    Cell 3

    This works fine.

    I rewrote the didSelectRowAtIndexPath method to the following, to get to different views on the individual cells, but it will always go to View3. Why?

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic

    if (self.view2ViewController == nil) {
    if (i == 1) {
    View2ViewController *view2 = [[View2ViewController alloc] initWithNibName:@”View1″ bundle:[NSBundle mainBundle]];
    self.view2ViewController = view2;
    [view2 release];
    if (i == 2) {
    View2ViewController *view2 = [[View2ViewController alloc] initWithNibName:@”View2″ bundle:[NSBundle mainBundle]];
    self.view2ViewController = view2;
    [view2 release];
    if (i == 3) {
    View2ViewController *view2 = [[View2ViewController alloc] initWithNibName:@”View3″ bundle:[NSBundle mainBundle]];
    self.view2ViewController = view2;
    [view2 release];

    [self.navigationController pushViewController:self.view2ViewController animated: YES];

    BTW: Cant we establish a board or forum system? That would be much better :)


  • http://icodeblog.com Brandon


    The variable that specifies which cell was selected is indexPath. So instead of using if(i==1), use if(indexPath == 1).

    Also, I think it is 0-indexed.

  • Bob Schoenburg

    How would I store indexpath in a variable?

    I tried an integer and got a “cast” error??

  • Uhu

    indexpath seems to be an Array. Indexpath ==1 doesnt. Work.

    Again: why dont we establish an board/Forum for discussion?

  • http://icodeblog.com Brandon


    I have put up a survey to see if more people are interested in a forum/discussion board on this site.

    Click here to go to it and vote.

  • http://www.iphonedev.jp/2008/08/08/iphone%e3%81%ae%e9%96%8b%e7%99%ba%e3%80%81%e3%81%a9%e3%81%93%e3%81%8b%e3%82%89%e3%82%b9%e3%82%bf%e3%83%bc%e3%83%88%ef%bc%9f/ iPhone Dev · iPhoneの開発、どこからスタート?

    [...] iPhone Programming Tutorial – Transitioning Between Views [...]

  • Ryan Sandberg

    Thanks for the great tutorial! I have to say that I just found out how touchy Xcode is. I put “mainbundle” instead of “mainBundle” and it totally crashed the view switching. Man. Either way, thanks! I’m well on my way to getting an iPhone app out there! :D

  • http://icodeblog.com Brandon

    Yea, it is case sensitive. Kind of a pain in the but, however it does force you to write better code. Thanks for reading!

  • http://icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/ iPhone Programming Tutorial – Populating UITableView With An NSArray | iCodeBlog

    [...] iPhone Programming Tutorial – Transitioning Between Views Blogroll [...]

  • J

    Is it possible to do the same app but instead of a UITableView simply use a UIView ? I cannot find any direction on a simple UIView with a Navigation Controller?

  • J

    Sorry – I meant with a Navigation Controller, Navigation Bar and a UIView.


  • Kaia

    How would you go about implementing the UITextView in this example for SQLiteBooks example from the sample apps from Apple. They use TableView but it only allows one line onf text to show. I would like to view multiple lines.

  • bobcubsfan


    what do you mean view multiple lines? the fruit tutorial posted here does that

  • Kaia

    Yes I did read the fruit tutorial ( I thought I actually posted on that page). I have tried to add the code in the fruit tutorial to the SQLiteBooks sample, but can not get it to work. “Multiple lines” meaning that in the SQLiteBooks example the cells are just tableviewcells and can nto display more than one line of data. I what to display a few lines of text in each cell (there being 4 cells on the view). In the fruit tutorial there is just one.

    sorry for posting in the wrong place.

  • bobcubsfan

    Maybe this will work:

    UITextView displays a region that can contain multiple lines of editable text. When a user taps a text view, a keyboard appears; when a user taps Return in the keyboard, the keyboard disappears and the text view can handle the input in an application-specific way. You can specify attributes, such as font, color, and alignment, that apply to all text in a text view.

  • Hadrian

    Another tut I enjoyed bro.

    Great stuff.


  • Jamshid

    Thanks man, Interface Builder is not obvious, this tutorial really brought everything together.

    I kept getting the main view until I compared my code to your tutorial — I hadn’t set initWithNibName. I was thinking xcode would tie the xib resource to the new view class by name.

  • Patrick

    Starting with your tutorial that had the connect button and text field that you put in your name and it said Hello …

    I tried to get it to transition from the “updateText” function into the View2. I got it all to compile and the updateText is working correctly but it is not pushing the View2. Was curious how to do that.


    I have a new motto for Apple.

    Apple knows all.. tells none.

  • Balagopal Ambalakkat

    Uhu asked:
    Why do I have to call View2ViewController with self.view2ViewController?

    View2ViewController is defined with Property declaration attribute of “retain”

    Specifies that retain should be invoked on the object upon assignment. (The default is assign.)
    This attribute is valid only for Objective-C object types. (You cannot specify retain for Core
    Foundation objects—see “Core Foundation” (page 56).)

    so calling self.View2ViewController will invoke the proper Setter method that handles the retain functionality.

    self.view2ViewController = X is equivalent to
    [View2ViewController setView2ViewController:X];

    More information on retain and memory management can be found in the below tuorial.

  • Balagopal Ambalakkat

    Forgot to say thanks for the tutorial.Hope to see more.

  • Patrick

    Yes BTW these tutorial have advanced me several hours as I cant believe how poor we have come along with programming. You look at the mess in either j2me or iphone as realize that we have not advanced software in the last 20 years. Just made in more terse….

    Thanks for your hours in this. keep it up !

  • Dick

    These are great tutorials!

    I wonder if you could point me in the right direction:

    I have an app working that:

    1) parses an XML file, extracting Video titles and URLS, then displays the titles in a Navigation Table View
    2) when you click on a title it invokes Safari with the URL so Safari plays the video

    This all works fine! But when the video ends, you must manually restart my app & repeat the above.

    What I would rather do is
    A) play the Video directly in my app
    B) when the Video is done or stopped, redisplay the Table View of videos.

    I have fiddled with the SDK MoviePlayer example but can’t figure out how to incorporate it into my app.

    An additional request would be to have:

    C) a view that is displayed when the app first starts and is parsing the XML — It could take several seconds. Ideally, this would display an image and a progress indicator. When finished parsing, switch to the Table View of Videos

    Any help will be greatly appreciated.



  • http://icodeblog.com Brandon


    I’m wandering if putting a UIWebView into your app would solve this problem. So instead of opening safari to play the video in a webpage, you could open the URL inside of a UIWebView.

  • Dick


    Thanks… I’ll have a look at that.

  • yali

    Hi, Brandon, Great work, it really get me started very quickly. I would like to see more of these tutorials, especially related the such cool features like file handling, GPS related programing, Open GL related.
    thanks very much.

  • http://HowtoaccomplishthiswithViewBasedapp? Brian

    I am very new and inexperienced with the iPhone SDK so please be patient. I have a View Based application and I am trying to get the views to change when a button is pressed. I ran this tutorial and I do not get any compile errors or warnings, but when I click on the button the new view does not show up. I know the button is wired correctly because I put a “printf” command in the action for that button and it shows up in the console window when I click the button in the simulator.

  • Adam

    Hi Brian, why don’t you post your codes so we can debug for you.

  • Lawrence

    Great tutorial. Just what I needed. Very clear and concise. What a great job. Keep up the good work.


  • nigel

    I agree with everyone else’s comments – great tutorial – has really helped things ‘click’ for me :-)

    Back in mid-Aug J asked about the use of the UITableView and whether it could be simply replace by UIView.

    Is that possible or is there any particular reason that the ‘Navigation’ template creates an application with a table in the main view?

    Thanks again for the tutorial

  • Daniel


    How do I get the name of the current visible view?

    Could you please send an answer to my email too?

    Very great site!

    Best regards

  • Monkey

    I’ve been trying to get this working for a whole day without luck. It took only a few minutes after I found your tutorial. Great job, add more tutorials!

  • http://icodeblog.com Brandon


    Thanks for reading. More tutorials to come… I should have some video tutorials in the near future.

  • Monkey

    If you were to add a text field to the view and connect it the view controller in IB:

    @interface MyViewController : UIViewController {
    IBOutlet UITextField* myTextField;
    // link to a text field using IB


    Do you have to release myTextField in the deallocator or
    will it get automatically released?

    -(void) dealloc {
    [myTextField release]; // required or not?
    [super dealloc];

  • Llll


    Great tutorial. How can I create a back button with some custom text (like “Back”) instead of the title of previous screen?


  • Corey

    @anybody with “what is….”

    Objective-C is a pretty advanced language with many non-obvious shortcuts and tricks to simplify code.

    Specifically, I see references to “properties”. Properties are a documented feature of Objective-C 2.0. An explanation of these can be found in Chapter 4 of the Objective-C 2.0 Language Guide. Which in turn can be found in the iPhone Developer Center.

    I would highly encourage all visitors to look up these types of questions and post more of the “hows” and “whys” here. Otherwise our new teacher may become quickly overwhelmed…

  • Ramya

    the same project i downloaded.but its not executing in the iphone simulator,which i doanloaded in iphone sdk.please help me.settings are correct..

  • superUser

    Can someone help me How to transit between views in view-based app.
    The code used in this tutorial is for navigation-based app

    Thanks in advance!

  • Ramya

    the projects says exit with status error 5.
    whats the meaning of it???
    All the projects are giving same error.
    what to do??
    plzzzzzzzzzzzzzzz help me…
    thank you

  • http://icodeblog.com Brandon


    Hundreds of people have completed these tutorials without a problem. So I am confident that there is no error. Have you downloaded the latest version of the sdk?

  • http://www.athenadesign.co.uk/blog/index.php/archives/58 iPhone Application And Website Development | Athena Design – The Lounge

    [...] Transitioning Between Views [...]

  • John


    I read in one of your earlier posts saying “you would not want to display an entirely different view depending on the cell clicked.” Why is that? Is it even possible to do? If so how would you do it. I’ve been trying to do this for ages now but can’t seem to find out how.


    P.S. I love your tuts. This site is way better then the other ones I’ve been on. See you guys around.

  • John


    Nevermind, I found out how… lol

    I always do this, omg….

    I ask someone for help and find out how to do it.

    It’s a curse.

    Catch ya later.

  • John

    Does anyone know how to go from a ViewController to a TableViewController???? Basically going from a viewcontroller to a another viewcontroller?

    I would like it to transition from one viewcontroller to another viewcontroller without using subviews.

    Any help would be appreciated.

  • Mihai

    I have an issue and i dont know how to handle it. I want to switch between views without pushing any buttons and with a delay of 4sec. I used sleep(); for that but looks like doesnt work at all.

    Before i removed the subView i used the sleep ….adding after my new view…didnt work…and so on and so on….

    If someone can help me …

    Any help would be appreciated.

  • http://icodeblog.com Brandon


    For this, look into using NSTimer. This would be a great solution to your problem.

  • Mihai

    Thx a lot. One more question…do you know or have you done some app with audio streaming?..or can you tell me from where to see some working code or tutorials or whatever. I’ve read the Apple documentations but will be much faster for me to work having an working app…


  • RL

    Just want to say thanks. Your tutorials (especially the Interface Builder stuff) are so much easier to follow than digging through the official docs. Keep up the good work!

  • Gevorg

    Any idea how we can accomplish the same thing using a button instead of the tableview? I created a button in the main view just like in your tutorial about linking the code with the interface builder view and placed all the code to switch to the next screen inside the mothod that’s called when the button is clicked and it isn’t working.

    Here is the code for that method:

    -(void) getDetails:(id) sender {

    if(self.detailsViewController == nil) {
    DetailsViewController *detailsview = [[DetailsViewController alloc] initWithNibName:@”Details” bundle:[NSBundle mainBundle]];
    self.detailsViewController = detailsview;
    [detailsview release];

    [self.navigationController pushViewController:self.detailsViewController animated:YES];


    If I put in other functionality inside the method to for example change the text of a label, it works, but switching the views doesn’t. I don’t get any build errors either.

  • MacE


    Did you ever get get the thing to move on to different Views working????

  • http://openideals.com/2008/10/27/links-for-2008-10-27/ links for 2008-10-27 | Nathan and his Open Ideals

    [...] iPhone Programming Tutorial – Transitioning Between Views | iCodeBlog This tutorial will focus on transitioning from one view to another. We will be utilizing Apple’s UINavigationController. (tags: iphone programming xcode tutorial) [...]

  • Vinit

    Hi there,
    I want to make table view the second view and the plain view as my first view plain, anybody have any idea how to do that ?

  • http://www.kylehayes.info/blog Kyle Hayes

    Thank you so much for these great tutorials. They have gotten me further than even the books that I have purchased. They are exactly what I have been looking for!

  • rich

    Brandon, I just want to say thank you soooooooo much for all your tutorials. I learn soooooooo much from them as I am just started playing with Ojective-c. Slowly I get to understand it. All these terminologies stuff really confuses me but I am slowly digesting from your tuts.

    I wonder if it’s possible for you to put the code in text insteand of png?

    Thanks again.

  • http://www.mikkolehtinen.com/iphone/iphone-developer-resources/ iPhone Developer resources | MikkoLehtinen.com

    [...] iPhone Programming Tutorial – Transitioning Between Views [...]

  • Koti

    This one is Excellent….. It is a great pleasure to have this type of information.. which will encourage the new Developers…..

  • SyD

    I found your your website yesterday……excellent tutorials.

    When I click on NEXT PAGE the program stops working and I get a __TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__.

    I’ve double check everything…I think !

    Now I guess I need an iPhone debugging tutorial. :)

    Where do I start looking, what are the most common mistakes people do ?

    Any sort of hints that could put me on the right track would be appreciated.

  • Rafael

    Hi Brandon. I am trying to accomplish the same effect but with a button and from a view based template. The only different thing I did is that I did not create a property for the second view, instead I just instantiate it on the fly and add to the stack as:

    -(IBAction) nextView:(id)sender{

    NextViewController *view2 = [[NextViewController alloc] initWithNibName:@”NextView” bundle:[NSBundle mainBundle]];

    [self.navigationController pushViewController:view2 animated:YES];

    [view2 release];


    But it does nothing. Do you an error with this approach or is it something else? The method is defintely getting called as I place a breakpoint to check and no exception is thrown.

    Thanks for the great tutorials, they are the best I found.

  • http://N/A Kieran

    I had this problem often when i began iphone programming because of being new to this memory management. Try to find something that you’ve released before u were done with it. That would cause that error. Good luck :)

  • Tim

    > The second part starts with a lower case “v“.
    > This is our variable name that we will use to
    > reference our ViewController

    *PLEASE* use different variable names that tell what they actually do.

    Don’t just change the CASE… then have to write a long explaination as to WHY.

    Did you run out of variable names? Can’t think of any? So you just keep reusing the same words… but mixing up the case (and expect us to remember them all)?

  • Allen

    Why do you have to make all your “code examples” as pictures?

    No one can ever cut/paste/use them.

    Simple text would be fine, instead.

  • http://www.ericthayne.com Eric Thayne

    Great tut, Brandon! Thanks a ton!

    One quick question. I’ve created a view-based application. Now I’ve created another view, but I want the program to open up in this new view, not the old one. How do I switch that?


  • Magnus

    Your tutorials are great, Brandon. Keep up the good work.

    I have a small question regarding this code, though:

    if ( self.view2ViewController == nil ) {
    self.view2ViewController = [[View2ViewController alloc] initWithNibName:@”View2″ bundle:[NSBundle mainBundle]];
    self.view2ViewController = view2;
    [view2 release];

    As far as I understand you allocate a new pointer (view2) and assigns it to self.view2ViewController. When view2 later is released, wouldn’t it leave self.view2ViewController pointing to an unallocated chunk of memory?

    This could explain the problem described by Uhu as well, since he would end up allocating and deallocating a view in the same memory area three times instead of having created three separate views…

  • Magnus

    Sorry, I copied the code from one of the other comments. It should have been:

    if ( self.view2ViewController == nil ) {
    View2ViewController *view2 = [[View2ViewController alloc] initWithNibName:@”View2″ bundle:[NSBundle mainBundle]];
    self.view2ViewController = view2;
    [view2 release];

  • Thikrat

    I think I may be missing something very obvious but here we go anyways. When it is time to add the back button, we simply add one line of code to the viewDidLoad method. If “self” is supposed to refer to the implicit object, then how does it know to set the title for the back button?

    To me, it makes more sense if the code looked like

    self.backButton.title = @”Back”;

    how does the line we used work?!?!? it seems to be a very ambiguous reference unless I am missing something IMPLIED. Thanks for the tutorials and help.

    self.title = @”Back”;

    - (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = @”Back”;

  • Tylor


    In the step “Connect The View To Code”, it asks to select “View2ViewController” from the drop down; but I don’t have that listed. Dose anyone know, from that little bit of information, where I have messed up the code. This is my second time at this tutorial because I keep getting stuck at this part (not seeing “View2ViewController” listed in the drop down). I did try just typing it in but I couldn’t connect it to the Files Owner. Any help would be greatly appreciated!

  • Steve

    Hey Brandon, I just discovered this site. What a wealth of information!!! Thanks for putting this up for us all.

    I did the tutorial and got it working, but then I tried to create a second view in my own application (a view based app) I have two view controllers and set things up just like in your tutorial but it not working, no errors – it just does not work. I trigger loading the second view with a button – should this work the same way or am I missing something???

  • http://www.rosscairns.com Ross

    Hi Brandon 
    Thanks for these tutorials!
    A prefect introduction to the iPhone.

  • sandeepiphone

    I created a View-Based Application in XCode. On the main view there is a UIButton. Then I created a second view called View2.xib which has the UILabel that says “Second View”.
    Now, all I want to do is display the second view when you click on the button. In the view controller class for the first view that contains the button I added.
    #import View2ViewController.h
    View2ViewController *View2;
    And I added the property for this. This class also has the IBAction for the button click event, which works just fine and executes when clicked. Now, in the .m file I obviously @synthesize View2; and begin to edit the button click method to display the view. Here is the code:
    if(self.View2 == nil) {
    View2ViewController *two = [[View2ViewController alloc] initWithNibName:@”View2″ bundle:[NSBundle mainBundle]];
    self.View2 = two;
    [two release];
    [self.navigationController pushViewController:self.View2 animated:YES];
    Everything complies, and runs. When I click on the button the application does NOT display the second view. Nothing crashes, but it doesn’t display the view. What am I doing wrong. I have everything linked up in Interface Builder. It’s quite basic.
    Is a “View-Based Application” not the correct type for doing this? Thanks everyone.

  • http://www.electricchalk.com electric chalk


    sandeepiphone — i am having exactly the same problem as you! I have followed the above guide, put the code into the h and m files of the current view on a “single view project”. It compiles, and runs but clicking the button does nothing.

    I think maybe we need to start with a “Windows Based Project”. But i wouldn’t even know how to create the first view.

    Brandon – This blog is amazing! Your tutorials are great. Can you release a book with everything in order :-)

    PLEASE make your next guide how to switch between views by clicking a button. (Lot of people on here seem to want the same!)

    Many thanks!

  • aQ

    just making a quick post to second electric chalk and every other developer who wants/needs a guide on how to switch between views by a button tab

    PD.. great job (: thx 4everything

  • Jagat

    Hi iCodeBlog [Sorry, I could not locate your name anywhere]

    I tried to follow this tutorial and it worked. Great, thank you.

    But I need a tab barat the down as well. So for this application IB did not allow me pull a tab bar to it. So I tried doing the same thing using viewcontroller instead of tableview controller. I created a view controller based app instead of navigation based app. I did put a table view on top of the view. The tableview worked. but navigation does not work. The code self.navigationController gave nil. Thats why the pushViewController function does not get called.

    Please advise me where I could have gone wrong
    thanks in advance

  • Ellen

    Hi Brandon,

    Thank you for posting your tutorials. I feel a little like an idiot for having owned a Mac for so long and not delving into Cocoa development before, as I would have been poised for the iPhone now, but better late than never. I have found your remarks in this tutorial especially timely when you remind the reader to check the case of types – XCode “help” is usually very helpful but sometimes it chooses the name with the wrong case and I missed it, but your reminder helped me to fix it before I had to find it the hard way.

    Thanks again,

  • SacMan

    I had been using all these tutorials just fine until I hit this one. I had been having a lot of the same issues mentioned above. I found 2 things I was doing wrong that may be what was tripping you up as well. The first may be obvious, but make sure that the name of your .xib file and the string that is being called in the didSelectRowAtIndexPath function is the same (View2 in the example above).

    Also, when creating the View2 view in interface builder BE SURE that you first select the Cocoa TOUCH category on the left hand side, so you don’t choose a non-touch Cocoa Custom View which won’t link up in interface builder.

    If you’ve done both of those correctly, then the View2ViewController class name will show up in the Interface Builder drop down list, otherwise it won’t at all.

  • http://darrell.brogdon.net Darrell

    @SyD I ran into the same problem and after looking at the console I found a hint at a solution here:

    Specifically I saw in the console where it said, “reason: ‘-[UITableViewController loadView] loaded the “View2″ nib but didn’t get a UITableView.’”.

    So I double-clicked View2.xib, clicked on the ‘View’ box (the one next to ‘First Responder’), clicked ‘Tools->Identity Inspector’, and changed the class from ‘UIView’ to ‘UITableView’.

    After doing that everything ran just fine.

  • jonathan

    i have been trying to connect the following UIView (the application in its usable form)-> UITableView (a settings menu type thing -> UIView (containing editable fields for the setting you clicked)
    and cant seem to get it to work, i used the fruit tutorial (UITableView -> UIView) )and got that working fine, but seem to get it to work starting from a UIView, any help on the topic or a point in the right direction?cant

  • James

    Sacman, you are a lifesaver! I was stuck on that for about an hour. Mainly that I didn’t select Cocoa Touch. :(

  • http://www.bengarvey.com Ben

    I’m absolutely loving these tutorials. Great work.

  • Graham F


    I’ve got a utility application and I’m trying to add extra views to the FlipSideView Controller, so they slide in and out.

    I’ve added the code and the extra objects as above, but I can’t get it to work correctly!

    Am I adding the code to the wrong area, should I be adding this code to the RootViewController and then get it to slide in the extra view to the Flipside View??



  • Amit6021

    Really too easy to understand and great help to new iphone

    Thanks for your support.

  • http://thinkjoke.servemp3.com Rush

    I was wondering how do I make it so that a second cell can say something else other than next view without changing the first cells text, how do i do this?

  • rrvs331

    How do I make it so that instead og using a UITable View i use a button and i start with a viewbased template, how would this work?

  • k182


    I am new in Iphone and your tutorials are the best I found on the web, clear and easy to understantd, but I will be like the others and ask for a solution if we want to do it by using a button.
    thanks, your site is really helpful

  • Andrew

    Why do you use the File’s Owner object as the Controller object? I usually make a Controller instance in Interface Builder and use that.

  • Tony

    These are some great tutorials and they are helping me learn the way I like to… hands on!

  • http://www.greenroad.com Ofer

    If you get this error:
    loaded the “View2″ nib but didn’t get a UIView….

    replace the *View2 at the following line with *view2 (small case) and it will work.

    View2ViewController *view2 = [[View2ViewController alloc] initWithNibName:@”view2″ bundle:[NSBundle mainBundle]];


  • Alfonso Mateos

    Thanks a lot for this concise tutorial, it shows the main concepts I was looking for, and it gave me the grasp I needed to self myself confident to start doing something with iPhone development.

    Thanks a lot from Spain.

  • http://ashwanik.blogspot.com Ashwani

    Thanks for this tutorial

    I have one problem please help me in that.

    I want to give home button in the tab bar in the bottom of the screen. One thing I can do , create a new instance of rootviewController and push it but I want reuse the already pushed root view. I have a navigation based application.

  • Jim

    Great tutorial!

    Thanks for taking the time to prepare the tutorials like these, much more useful than the articles on the iPhone Dev Center. :-)

  • Delwyn

    Hi, I am a beginning in this iPhone SDK. May I know how do I add another view for example View3 to the table?

  • Delwyn

    Hi, I am a beginner in this iPhone SDK. May I know how do I add another view for example View3 to the table?

  • augustine

    I have iphone SDK 3.0. When the similator runs it crashes when it attempts to execute the following line :

    [self.navigationController pushViewController:self.view2ViewController animated:YES];

  • Samuel

    Nice tutorial, Everything work good until went I go back to Hello view when try to go back to next view it wont go. Any idea

  • Chris

    Whenever I try the code, either the source code or the code I typed up, it ends with an uncaught exception after I click the cell.

  • http://joshuarussell.net Joshua Russell

    Hi, i have followed the above guide step by step, however when i compile the code, in the debug console, i am getting the following error:

    *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[UIViewController loadView] loaded the “View2″ nib but no view was set.’

    Any ideas?

    I downloaded the source code from this site, and it compiles fine, but i want to know what im doing wrong.

    i you know, email me! –

    thanks :)

  • Scott Calafiore

    Using SDK 3.0 get an error on line self.View2ViewCotroller = view2;
    error: request for member ‘View2ViewCotroller’ in something not a structure or union
    on build and go.
    It also won’t let me connect the new referencing outlet from View2 View Controller when I drag it over nothing happens any ideas?

  • http://blog.media-scientific.com Philipp

    Thanks al lot for your Tutorials.
    But I need help.

    I wont show a nother View when i click on a button in a Window. I have a window, i have a button and a seccound view.

    But i can’t understand what i must do to implement another seccound view.

    You implement an if-condition at the method didSelectRowAtIndexPath. In my Case i wont have this method.

    Please can you say what method i need to change.

    Thanks a lot…

  • http://- Santanu.K

    Hi Brandon, Thnks fr the Tutorials! In this case, after adding View2ViewController.m to the project, and when I’m trying importing the View2ViewController.h in the RootViewController.h file, its repeatedly giving me error that there is no such file or directory named View2ViewController.h! I’ve checked the typos too.. but no luck! Can you suggest?

  • http://- Santanu.K

    Ah dmn! I got a silly mistake. I tried import as #import , instead of #import “View2ViewController.h>… dmn! It removes the errors now..

  • http://- Santanu.K

    Ah dmn! I got a silly mistake. I tried import as #import , instead of #import “View2ViewController.h”… dmn! It removes the errors now..

  • SteveH

    After adding all the code, I get in RootViewController.m: “warning: ‘setText:’ is deprecated….”

    Further down I get:
    “error: ‘view2′ undeclared (first use in this function)”

    I’m not sure how to “declare” it.


  • SteveH

    OK, I got my errors sorted out. But how do I change the second view’s text from “view 2″ to something else? I can find the string “view 2″ anywhere.

  • SteveH

    I see; it comes from the Text field in the label attributes in Interface Builder!

  • Vamsi

    Great tutorial. I got stuck at the connection inspector.
    When I try to connect the view2, I drag the referencing outlet on to File owner object, it does not give me drop down of view.
    Can you help me on what might be wrong?

  • Jim

    Nice tutorials, they’ve been really helpful!

  • Chris


    when I try to connect the View2 to “File’s Owner” it doesn’t work. The File’s Owner Object is not being highlightet the way it usually is when I connect something. Why do you think that is? Is there any way to work around this?


  • Sab


    Absolutely great tutorial and very helpful for what i am trying to do. I just have a question, how do you add a webview the view2 and when we click NextView it opens a link in the View2 which has a webview on it. I will greatly appreciate if you could help me with this it is killing me. I am new ti iPhone and rely on the help of others. Many thanks in advance


  • Imtiyaz

    Thanks again for your great effort.

    I am not able to understand how back button “Hello” got added to View2.

    - (void)viewDidLoad {
    self.title = @”Hello”;

    sets the title of Table View of RootViewController, I have few queries related to this

    1, How back button “Hello” is added to View2.
    2, How behaviour “back to root view” on “Hello” button is added.

    Thanks in Advance.

  • Amar

    Can you tell how can we switch views by clicking a button.

  • A.C.

    This demo is broken with Snow Leopard and Xcode 3.2. As Chris and Vamsi point out above, you can’t connect the view2′s referencing outlet to the File’s Owner. The connection attempt is rejected, not sure why…

  • Shay


    In SDK 3.0 there was a problem with the Identity inspector when trying to put “View2ViewController” in the class identiry.

    Then in the Connections Inspector, the “view” didn’t show after dragging from the “New Referencing Outlet” to the file’s Owner.

    Instead, when I created the subclass in Xcode, I did check the “With XIB for user interface”. The xib was created as “View2ViewController” instead of View2 as in the tutorial.

    Then changing the


    and it did work ok.

  • Shay

    Hi again.

    At the first time I’ve created a “Cocoa” view in the Inteface Builder. While the tutorial say to create “Cocoa touch” view.

    Therefore, I couldn’t find the “View2ViewController” Class Identity in the Identity inspector.


  • Judi Smith

    Hello Brandon,

    Thank you for the wonderful tutorials. Great asset to jump start iPhone programming. I have one small question. I’ve read through the comments and I don’t see it answered.

    The code all works just fine. But when I switch to view 2, the words appear in the middle of the view rather than at the top which is where I placed the label in IB.

    What would be causing that?

  • Yild

    Hello Brandon,

    Thanks for setting up such nice tutorials, everything is very clear and helpful.

    However, some of the options are out of date in this tutorial with the release of Xcode 3.2

    One of the option doesn’t work is: in the Connections Inspector, the “view” doesn’t show after dragging from the “New Referencing Outlet” to the file’s Owner.

    Please update….

  • http://leonardrumery.com Leonard

    Your tutorials are great, because your explanations are clear. However, I’ve put in the code in this tutorial (up to the addition of the back button) including connecting view2 to the File’s Owner view, and here’s what happens: First screen presents correctly; when I click on Next View cell, the program terminates without showing me the next view. I’ve checked the code several times. Any suggestions?

  • saiena

    Thanks, Darrel. You solution worked for me as well.

  • JT

    Yild… I am using xcode 3.2 with iphone sdk 3.1 and 3.0 loaded. I didn’t see any problems with the example.

    The only hiccup you may see is the deprecated command setText. Darrel’s code can be updated to the following if you want to get rid of the pesky warning message during build&debug:

    [cell.textLabel setText:@"Next View"];

    Otherwise, it still runs just fine.

  • http://inzolo.com Dustin Davis

    I had the same problem Leonard described. I found the problem though. I named the xib “View 2″ instead of “View2″ (notice space)

    So I updated this code to initWithNibName:@”View 2″ and it worked.

    Thanks for the tutorials!

  • Jens Fudge

    If Dustins explanation didnt help you.. I had the same problem, but I discovered I had a typo in didSelectRowAtIndexPath:
    As far I as I can see its a constructor creating an instance of View2ViewController.. My typo was in bundle, that I incorrectly spelled “bunde” (missing l)

  • Jens Fudge


    Thanks for these tutorilas, they are really great!!

    I do have some questions for you:

    1) The bundle thing in the constructor for view2, what’s that?

    2) I notice there is an asterisk in the variable name sometimes, and sometimes not. This confuses me. Is it when declaring variables they must be with an asterisk (*)?

    3) In didSelectRowAtIndexPath you first construct an instance of View2ViewController named *view2.
    Then you assign it to the property view2ViewController, which makes perfekt sense. But then you release view2, which I dont understand. Here’s why:
    The programming languages I am used to (Delphi and C#, I know, both running on Windows), would treet this as two pointers or handles to the same instance. By releasing either one of these pointers [view2 release], the object would be released from memory and the other pointer/handle/variable/property would be pointing to invalid or undefined memory.
    So the question here is, does the assignment in fact make a complete copy of said instance?

    4) The self keyword in RootViewController, which object does this refer to? Is it the application itsself, or the instance of the RootViewController?

    Once again. Fantastic tutorials!!

    Best regards
    Jens Fudge

  • Nate Smith

    The tutorials on this site are fantastic. Thank you for your tremendous contribution to the iPhone development community.

  • Vincent Chanliau


    First thanks for the great tutorials…

    Something I don’t understand in this one… it’s about the back button…

    How come setting the title property of the rootview make it a button that goes back to this view….

    maybe a stupid question but I don’t get it….

  • j

    Very informative.

    FYI, in version 3.1.2, it was necessary to uncomment the viewDidLoad method.

  • j


    I tried to do a transition on a window-based app & nothing seemed to work.

    Can you provide some tips for transitions outside of navigation-based apps?


  • http://www.entdyn.com Korky Kathman

    First, Brandon – very nice tutorials and indeed you should think about republishing them into some published form, if just in a PDF.

    Not sure if you are still addressing questions, since I haven’t seen you respond since Sept, 2008, but I was trying to follow Uhu’s explanation of iterating the cells and could never get it to work. If you have time, (or anyone else does) it would be nice to see how that got implemented . Most specifically, the iterative loop in the – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    method to generate multiple rows.


  • Jason FB


    In RootViewController.m, don’t we need to release view2ViewController when the RootViewController itself is released?

    - (void)dealloc {
    [view2ViewController release];
    [super dealloc];

  • http://www.comicearth.com Brian

    Thanks so much for this website, and I’m more interested in the game tutorials but for now am learning how to program for the iPhone before I start on my game.

    I’ve tried this out but keep getting an error when navigating to the other view, When I go to debug it says:


    and focus on 0x01c1b004 leave

    I’ve looked over the code again and again but since I’m new to this can’t seem to find why this is happening. :S

    Any thoughts?

  • Shankar

    I saw the same error. The way I got around it is that in the line View2Controller *v2 = [[View2Controller alloc] initWithNibName:@”View2″ bundle:nil]; the initWithNibName needs to be the same name as the View2 xib file. If not, it throws the above exception

  • http://www.dremsus.com/blog/?p=39 Anil Anjan » iPhone Tutorial5–Views Transition

    [...] Set Up The Transition To The View [...]

  • Stephen Smith

    I would like to have a different view controller with my Nav Controller based app for preferences setting. So I don’t want use pushViewController : animated to display it. I want it to flip in. I know how to do this in a non-Navigation controller based app, but am having trouble getting it to work in a Nav Controller based app. Any hints?

  • http://thebesttunes.viviti.com Luke

    thanks for the great tutorial. I was wondering if you could possibly help me. i am creating a joke app, which i am guessing is going to require many many views, so when the person touches next – previous it will follow their progress. How would i go about this?

  • Brian

    I have the same problem as below..

    “This demo is broken with Snow Leopard and Xcode 3.2. As Chris and Vamsi point out above, you can’t connect the view2’s referencing outlet to the File’s Owner. The connection attempt is rejected, not sure why…”

    This would have been a nice tutorial if it worked .. should provide a warning so people can skip this or update your tutorial..

  • shishir

    i hv a problem,i hope u can help me out.

    i hv a view with a button,

    wat i am trying to do is,on button click ,new view should come “with tab bars.”

    i created 1 tab bar controller and added correspoing nibs to tab bars.

    but i m not able to call this tab bar controller view from my previous view whoch is containing a button.

    hope i sound clear,with my problem.

    looking forward for a quick reply

    shishir mishra

  • http://magichand.org/?p=212 magichand Interaction -魔法交互 – iPhone Application And Website Development: All Tools And Tutorials You Need

    [...] Transitioning Between Views [...]

  • Larrybud

    As someone who is versed in .NET (vb and c#) iPhone development has been a big learning curve. Kinda like going back in time and doing win 3.1 programming. Lots of handing events yourself, wiring things up, etc. Lots more lines of code to mess around with (not a bash, just an observation).

    One thing that I’ve come across is the naming conventions that iPhone (and maybe Apple?) developers seem to use, and that is just changing the case of a variable two, maybe three times. Over and over I see this in blogs, tutorials, and even the iPhone SDK (maybe that’s where everyone got it from??)

    Terrible programming practice, IMO, and makes readability of the code incredibly difficult, not to mention the confusion the IDE has when trying to guess which variable you want.

    If the nature of the language is that it’s case sensitive, that’s fine, but essentially changing the meaning of a variable because of the case doesn’t make any sense. Could you image the confusion if these three sentences all meant something different:

    The sun is yellow.
    the Sun is yellow.
    The Sun Is Yellow.

    While the first sentence is the correct case, all three mean the same and everyone knows what I’m saying by looking at any three.

    Anyway, my two cents. It’s an interesting platform to develop on, and I would image Apple will hopefully increase the ease of use of the IDE. For example, instead of wiring things up manually, there’s no reason I shouldn’t be able to click on a button (for example) in UI builder, choose an event, and the function outline be automatically written and wired for me instantly.

  • Balzidev

    Thanks a lot. This is the best tutorial for iphone

  • Balzidev

    This is one of the best tutorial for iphone dev.

    Wanted to add some thing. If you get error about “@synthesize view2ViewController” not declared after your build and go then recheck your view2ViewController.h file again


  • http://www.dremsus.com/?p=39 i-Tech » iPhone Tutorial5–Views Transition

    [...] iPhone Tutorial –Views Transitioning [...]

  • http://leondoornkamp.nl Leon

    Great simple yet effective tutorials. Keep up the good work!

  • Konrad

    THERE’S A BUG (?)!
    Hi all. The problem is – when you click the Next View, then get to the View 2, and klick the back button (Hello), then you can’t repeat this action. The Next View row simply do nothing after that. Could someone tell me what i have to do to change this?

  • Konrad

    Nevermind, It’s working. Now could someone explain me how to add second row showing second view?

  • fluvly

    Brandon, just one question… why create a new view when a new xib file gets automatically added when you’re creating a new view controller class?

  • Ryan

    I am new to iPhone programming and I am trying to figure out how to do the opposite of what you posted about.
    I want to start off with a view contoller and click a button on the screen and then it will show a TableView with a back button. Is that possible?

    Basically what I want to do is have a main screen that the user can enter search criteria and then the next page will bring up the search results in a long list. The user can think click on a row in the table that will take them to another UIView.

    UIView -> UITableView -> UIView

  • ejaa

    how i can make many views.
    on thhis example, u just show only one view.
    i cant imagine how to do that
    thnks for advanced

  • opéka

    Thanks a lot, that was so useful for me :)

  • Rahul

    hi, I tried the example starting with a ViewBasedApplication Template,Now I had following problems
    1) You did not show to include tany code in View2ViewController.m whereas the code that i downloaded had
    – (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    this function so I added this to my controller.
    2) I used following code to trace the function calls
    NSLog(@”<<<<Entering %s <<<<<",__PRETTY_FUNCTION__);
    though it shows Entering and exit from -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    still the next view doesn't come up.

    Reply at ur ease

  • spoolup

    great tut!
    how could it be modified so that when you go to the cell and press the next action is a uiwebview with a url (different url on every cell) with a back button?

  • Bivins

    Thanks for this great tutorial .Please I have a question to ask as I am a newbie and would like to make an app where I have the multiple pages but my problem is that I do not know how to get read only text onto the pages or views. For instance if I want to get for example these italics paragraphs:

    “The iphone is a smart phone which has so many capabilities and functions. Its introduction haas open so many doors of opportunities to many developers who would otherwise have died and would have been buried without been known.
    Get the iPhone now and join the band wagon. Don’t be caught off guard. The world is moving so move with it .”

    of read only text onto view 2 above in your tutorial above. Please I will like to know the steps to take to get this done .
    Thanks a lot.

  • Adel

    Your post solves a problem was irritating me for the whole day

    I didn’t set title for RootViewController so there was no back button.

    Thanks very much

  • Shankar

    first “Hello world” application run perfectly,even for navigation tutorial on click cell it navigate but second transition does not show.

    I get output on console is as follow:

    2010-08-26 19:14:49.989 Hello World[3473:207] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Could not load NIB in bundle: ‘NSBundle (loaded)’ with name ‘View2.”
    *** Call stack at first throw:
    0 CoreFoundation 0×02393919 __exceptionPreprocess + 185
    1 libobjc.A.dylib 0x024e15de objc_exception_throw + 47
    2 CoreFoundation 0x0234c078 +[NSException raise:format:arguments:] + 136
    3 CoreFoundation 0x0234bfea +[NSException raise:format:] + 58
    4 UIKit 0x004a8af8 -[UINib instantiateWithOwner:options:] + 2024
    5 UIKit 0x004aa4b5 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
    6 UIKit 0x00361fbb -[UIViewController _loadViewFromNibNamed:bundle:] + 70
    7 UIKit 0x0035fcd1 -[UIViewController loadView] + 120
    8 UIKit 0x004b6827 -[UITableViewController loadView] + 80
    9 UIKit 0x0035fbab -[UIViewController view] + 56
    10 UIKit 0x0035e050 -[UIViewController contentScrollView] + 42
    11 UIKit 0x0036ddf7 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
    12 UIKit 0x0036c4ff -[UINavigationController _layoutViewController:] + 43
    13 UIKit 0x0036d6c3 -[UINavigationController _startTransition:fromViewController:toViewController:] + 326
    14 UIKit 0×00368329 -[UINavigationController _startDeferredTransitionIfNeeded] + 266
    15 UIKit 0x0036f3a0 -[UINavigationController pushViewController:transition:forceImmediate:] + 876
    16 UIKit 0x003681c3 -[UINavigationController pushViewController:animated:] + 62
    17 Hello World 0×00002275 -[RootViewController tableView:didSelectRowAtIndexPath:] + 295
    18 UIKit 0×00329718 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
    19 UIKit 0x0031fffe -[UITableView _userSelectRowAtIndexPath:] + 219
    20 Foundation 0x00036cea __NSFireDelayedPerform + 441
    21 CoreFoundation 0x02374d43 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
    22 CoreFoundation 0×02376384 __CFRunLoopDoTimer + 1364
    23 CoreFoundation 0x022d2d09 __CFRunLoopRun + 1817
    24 CoreFoundation 0x022d2280 CFRunLoopRunSpecific + 208
    25 CoreFoundation 0x022d21a1 CFRunLoopRunInMode + 97
    26 GraphicsServices 0x02bf82c8 GSEventRunModal + 217
    27 GraphicsServices 0x02bf838d GSEventRun + 115
    28 UIKit 0x002c5b58 UIApplicationMain + 1160
    29 Hello World 0x00001db4 main + 102
    30 Hello World 0x00001d45 start + 53
    terminate called after throwing an instance of ‘NSException’

    Pls help.

  • http://none Jef

    terminate called after throwing an instance of ‘NSException’

    All your tutorials look great, however they seem old when using iOS4,
    Things like cell setText are deprecated now, and even more the app i write according to your howto/tutorial crashes when going to view2.

    Could you either way or fix these tutorials or write new up to date ones, because following yours when using iOS4 is very frustrating.

    other then that, great stuff man.

  • jeno

    Hello Brandon,

    Thanks for such a wonderful tutorial. It really help me a lot in my learning!

    1 thing that I don’t understand:
    Why do we need to create 2 instances of the same object (View2ViewController)

    In RootViewController.h file:
    View2ViewController *view2ViewController;

    In RootViewController.m file:
    View2ViewController *view2 = [[View2ViewController alloc] …


  • Edgar

    Thanks for the tutorials.
    But I fail to add the @”Hello”; button, is structure different as I using xcode 3.2.4 in IOS4?

  • David

    Has there been a fix for ios4? I am having the same problem with it crashing.

  • Abbas

    Was having trouble with my code. This tutorial really helped me a lot in getting my simple program up and running.. Thanks a LOT. Thanks a LOT. May GOD bless you.

  • http://www.spectere.net/ Ian

    Not sure if you’ll read this, but what the hey:

    You’re not creating two instances of the object. In the header (.h) file, you’re declaring the variable. Basically, this is telling the compiler that you intend to use a pointer with the name view2ViewController, and that it will point to an object with the type View2ViewController. That alone doesn’t create an instance of it.

    The real magic happens in the implementation (.m) code, specifically when you use the “[View2ViewController alloc]” statement. That is what actually creates an instance of the class and assigns the instance to the view2ViewController pointer. Until you allocate the object and assign its address to that pointer, you won’t be able to use the view2ViewController pointer for anything meaningful.

  • Mange

    Great tutorial! Straight to the point. Thanks

  • Rick

    cell.textLabel.text = @”Next View”;

  • Calla

    Thanks so much! This was so helpful! Will you please say how to add a second cell, not only one that says “Next view”?

  • Camid

    Very helpful! How can I add more views??
    Thank you.

  • http://www.digital-canopy.com Brian Kuyath

    I ran into this too. The problem I was having was when I returned back to Interface Builder to connect the code to the view. After double clicking on View2.xib to reopen Interface Builder, the steps tell us to click on File’s Owner, then Tools >> Connection Inspector, and then connect “view” to File’s Owner by dragging “view” to “File’s Owner”. I could not get this to work, and when I built the application, I would receive an exception error.

    To resolve this, I went back into Interface Builder and clicked the “View” icon. Then went to Tools >> Connection Inspector. Under “Reference Outlets” I saw no referencing outlets, so next to “New Referencing Outlet” I clicked on the empty circle and dragged it to “File’s Owner”. After I did this, “view” appeared on top pof the “File’s Owner” icon. I clicked “view” and it appeared connected to “File’s Owner” under “Reference Outlets”. I saved the update, returned to Xcode, clicked “Build and Run” and :: presto :: it worked!

  • http://www.my-digital-source.ca Wowplaya

    Thanks for the tutorial. Very helpful. Looks like alot of work though haha.

    Whats not alot of work is fixing a broken iPhone!

    You can fix your own broken iPhone in just 30 minutes!