iPhone Programming Tutorial - Transitioning Between Views
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
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:
- Add A New View
- Add A View Controller
- Set Up The Transition To The View
- Connect The View To The Code
- Add A Back Button
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!
- Posted by Brandon on 3 Aug 2008 in Interface Builder, iPhone Programming Tutorials
- Digg |
- Del.icio.us |
- Stumble |
117 Responses
Ricky Says:
August 3rd, 2008 at 6:42 pm
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.
Brandon Says:
August 3rd, 2008 at 6:52 pm
Ricky,
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 Says:
August 3rd, 2008 at 9:15 pm
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 Says:
August 4th, 2008 at 5:39 am
Hello!
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 ![]()
Thanks in advance!
@Tyten Says:
August 4th, 2008 at 6:46 am
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 Says:
August 4th, 2008 at 7:42 am
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!
Brandon Says:
August 4th, 2008 at 8:10 am
Tyten,
Glad to see you got it working! I went ahead and included the source code anyway.
Adam Says:
August 4th, 2008 at 9:23 am
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 Says:
August 4th, 2008 at 10:52 am
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!
Brandon Says:
August 4th, 2008 at 11:48 am
Adam:
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.
Tyten:
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 Says:
August 4th, 2008 at 12:21 pm
@brandon
I think your next tutorial is exactly what I want! ![]()
Keep up the great work! you’re helping a lot of people …
Neb Says:
August 5th, 2008 at 5:24 am
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.
Bob Schoenburg Says:
August 5th, 2008 at 5:34 pm
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 Says:
August 5th, 2008 at 5:49 pm
Found my error! One line of code was out of place. Works great now!
Pulkit Says:
August 6th, 2008 at 1:29 am
Hi,
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?
Thanks,
Pulkit
Uhu Says:
August 6th, 2008 at 1:47 am
Hallo,
thanks for the good tutorial. I have some questions about it though:
What does the @property (nonatmic, retain) statemant do exactly?
and:
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 Says:
August 6th, 2008 at 4:48 am
Uhu:
@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 Says:
August 6th, 2008 at 5:00 am
Aah thank you. Is there a docu for the property thing?
I do not exactly know, why this @property has to be declared.
Adam Says:
August 6th, 2008 at 7:45 am
PulKit:
I guess you can start with a View-Based Application template. Then create a new view and Subclass “UITableViewController”.
Brandon Says:
August 6th, 2008 at 8:12 am
@Pulkit
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…
Brandon Says:
August 6th, 2008 at 8:28 am
@Uhu
This blog has a good explanation of properties. It might be useful to you.
I haven’t found any formal documentation on properties.
Pulkit Says:
August 6th, 2008 at 8:42 am
Thanks for the help Brandon and Adams can you point some links which talks about “Navigation Controller” in detail?
Uhu Says:
August 6th, 2008 at 9:07 am
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
Thanks
Brandon Says:
August 6th, 2008 at 10:15 am
@Uhu
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 Says:
August 6th, 2008 at 10:32 am
How would I store indexpath in a variable?
I tried an integer and got a “cast” error??
Uhu Says:
August 6th, 2008 at 2:42 pm
indexpath seems to be an Array. Indexpath ==1 doesnt. Work.
Again: why dont we establish an board/Forum for discussion?
Brandon Says:
August 6th, 2008 at 3:34 pm
@Uhu
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.
iPhone Dev · iPhoneの開発、どこからスタート? Says:
August 7th, 2008 at 6:10 pm
[...] iPhone Programming Tutorial - Transitioning Between Views [...]
Ryan Sandberg Says:
August 8th, 2008 at 9:07 am
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!
Brandon Says:
August 8th, 2008 at 12:31 pm
Yea, it is case sensitive. Kind of a pain in the but, however it does force you to write better code. Thanks for reading!
iPhone Programming Tutorial - Populating UITableView With An NSArray | iCodeBlog Says:
August 12th, 2008 at 12:19 pm
[...] iPhone Programming Tutorial - Transitioning Between Views Blogroll [...]
J Says:
August 13th, 2008 at 7:24 pm
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?
Thanks!
J Says:
August 13th, 2008 at 7:25 pm
Sorry - I meant with a Navigation Controller, Navigation Bar and a UIView.
Thanks.
Kaia Says:
August 17th, 2008 at 11:19 am
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.
Thanks,
bobcubsfan Says:
August 17th, 2008 at 1:44 pm
Kaia,
what do you mean view multiple lines? the fruit tutorial posted here does that
Kaia Says:
August 17th, 2008 at 3:24 pm
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 Says:
August 17th, 2008 at 5:02 pm
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.
Jamshid Says:
August 18th, 2008 at 1:05 am
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 Says:
August 19th, 2008 at 3:50 pm
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.
Thanks.
I have a new motto for Apple.
Apple knows all.. tells none.
Balagopal Ambalakkat Says:
August 19th, 2008 at 3:52 pm
——
Uhu asked:
Why do I have to call View2ViewController with self.view2ViewController?
—-
View2ViewController is defined with Property declaration attribute of “retain”
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 Says:
August 19th, 2008 at 5:26 pm
Forgot to say thanks for the tutorial.Hope to see more.
Patrick Says:
August 19th, 2008 at 6:35 pm
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 Says:
August 20th, 2008 at 7:54 pm
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.
TIA
Dick
Brandon Says:
August 20th, 2008 at 8:38 pm
@Dick
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.
yali Says:
August 23rd, 2008 at 10:43 am
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.
Brian Says:
August 26th, 2008 at 11:29 am
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 Says:
August 26th, 2008 at 8:13 pm
Hi Brian, why don’t you post your codes so we can debug for you.
Lawrence Says:
September 3rd, 2008 at 1:57 pm
Great tutorial. Just what I needed. Very clear and concise. What a great job. Keep up the good work.
L.
nigel Says:
September 10th, 2008 at 5:00 pm
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 Says:
September 15th, 2008 at 12:09 am
Hello!
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
Daniel
Monkey Says:
September 19th, 2008 at 8:51 am
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!
Brandon Says:
September 19th, 2008 at 8:56 am
Monkey,
Thanks for reading. More tutorials to come… I should have some video tutorials in the near future.
Monkey Says:
September 20th, 2008 at 1:17 am
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
}
…
@end
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 Says:
September 21st, 2008 at 5:05 am
Hi.
Great tutorial. How can I create a back button with some custom text (like “Back”) instead of the title of previous screen?
Thanks
Corey Says:
September 21st, 2008 at 11:01 pm
@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 Says:
September 23rd, 2008 at 5:11 am
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 Says:
September 23rd, 2008 at 2:01 pm
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 Says:
September 25th, 2008 at 3:34 am
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
Brandon Says:
September 25th, 2008 at 7:21 am
@ramya
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?
iPhone Application And Website Development | Athena Design - The Lounge Says:
September 29th, 2008 at 12:29 am
[...] Transitioning Between Views [...]
John Says:
September 29th, 2008 at 5:31 pm
@Brandon
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.
Thanks…
P.S. I love your tuts. This site is way better then the other ones I’ve been on. See you guys around.
John Says:
September 29th, 2008 at 5:39 pm
@Brandon
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 Says:
September 29th, 2008 at 6:29 pm
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 Says:
October 7th, 2008 at 6:56 pm
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.
Brandon Says:
October 7th, 2008 at 8:38 pm
@Mihai,
For this, look into using NSTimer. This would be a great solution to your problem.
Mihai Says:
October 8th, 2008 at 10:31 am
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…
Thanks!
RL Says:
October 11th, 2008 at 10:56 pm
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 Says:
October 14th, 2008 at 4:11 pm
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 Says:
October 23rd, 2008 at 3:42 pm
@Uhu
Did you ever get get the thing to move on to different Views working????
links for 2008-10-27 | Nathan and his Open Ideals Says:
October 27th, 2008 at 10:04 am
[...] 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 Says:
November 5th, 2008 at 11:33 am
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 ?
Kyle Hayes Says:
November 23rd, 2008 at 12:36 am
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 Says:
November 26th, 2008 at 8:04 am
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.
iPhone Developer resources | MikkoLehtinen.com Says:
November 26th, 2008 at 4:19 pm
[...] iPhone Programming Tutorial - Transitioning Between Views [...]
Koti Says:
December 12th, 2008 at 5:20 pm
This one is Excellent….. It is a great pleasure to have this type of information.. which will encourage the new Developers…..
SyD Says:
December 22nd, 2008 at 2:16 pm
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 Says:
December 23rd, 2008 at 3:09 pm
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.
Kieran Says:
December 24th, 2008 at 8:42 am
@SyD
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 Says:
January 1st, 2009 at 11:32 am
> 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 Says:
January 1st, 2009 at 11:36 am
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.
Eric Thayne Says:
January 3rd, 2009 at 3:15 am
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?
Thanks.
Magnus Says:
January 3rd, 2009 at 2:29 pm
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 Says:
January 3rd, 2009 at 2:34 pm
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 Says:
January 4th, 2009 at 9:24 am
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 Says:
January 6th, 2009 at 6:13 am
Hello!
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 Says:
January 6th, 2009 at 9:16 pm
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???
Ross Says:
January 8th, 2009 at 11:28 am
Hi Brandon
Thanks for these tutorials!
A prefect introduction to the iPhone.
sandeepiphone Says:
January 9th, 2009 at 11:54 pm
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.
electric chalk Says:
January 17th, 2009 at 4:09 pm
Hi,
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 Says:
January 29th, 2009 at 5:31 pm
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 Says:
February 3rd, 2009 at 5:58 am
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
Jagat
Ellen Says:
February 3rd, 2009 at 8:31 pm
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,
Ellen
SacMan Says:
February 6th, 2009 at 12:22 pm
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.
Darrell Says:
February 9th, 2009 at 2:09 am
@SyD I ran into the same problem and after looking at the console I found a hint at a solution here: http://oedalpha.springnote.com/pages/1688324
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 Says:
February 13th, 2009 at 3:51 am
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 Says:
February 14th, 2009 at 10:27 pm
Sacman, you are a lifesaver! I was stuck on that for about an hour. Mainly that I didn’t select Cocoa Touch.
Graham F Says:
February 16th, 2009 at 4:24 am
Hi,
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??
Thanks
Graham
Amit6021 Says:
February 18th, 2009 at 8:27 am
Really too easy to understand and great help to new iphone
developers.
Thanks for your support.
Rush Says:
March 5th, 2009 at 7:41 pm
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 Says:
March 6th, 2009 at 2:30 pm
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 Says:
March 10th, 2009 at 3:45 pm
Hi
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 Says:
March 19th, 2009 at 6:36 am
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 Says:
March 21st, 2009 at 11:18 am
These are some great tutorials and they are helping me learn the way I like to… hands on!
Ofer Says:
March 22nd, 2009 at 10:32 am
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]];
Ofer
Alfonso Mateos Says:
April 3rd, 2009 at 4:09 pm
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.
Ashwani Says:
April 17th, 2009 at 12:17 am
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 Says:
April 19th, 2009 at 4:18 am
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 Says:
May 12th, 2009 at 4:31 am
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 Says:
May 12th, 2009 at 4:31 am
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 Says:
May 18th, 2009 at 8:31 am
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 Says:
May 21st, 2009 at 5:27 pm
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 Says:
June 16th, 2009 at 1:37 pm
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.
Scott Calafiore Says:
June 28th, 2009 at 4:18 pm
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?
















