Iphone Programming Part 2 – Intro to Views

Welcome to iphone programming part 2, introduction to views. In this tutorial I’ll go over creating an application from scratch that transitions between 2 different views using the iphone’s built in UINavigationController and the navigation stack. If you haven’t done so already, I suggest reading through my first iphone article. If you already have a basic understanding of how to create an iphone application and hooking up IBOulets and IBActions in interface builder then you can head straight into this article. So let’s jump into it, and as always if you have any questions feel free to leave a comment and I’ll get back to you.

Let’s start out by opening up XCode and creating a new window based application from the standard iphone OS templates. Save the file to wherever you save your iphone files to and create.

Once you create and save your new window-based application, open up the classes and resources folders in the XCode folder explorer. Open up the ViewDemoAppDelegate.h and insert the following code…

//
//  ViewDemoAppDelegate.h
//  ViewDemo
//
//  Created by Mark Hazlett on 10-03-26.
//  Copyright Code Perspective 2010. All rights reserved.
//
 
#import <UIKit/UIKit.h>
@interface ViewDemoAppDelegate : NSObject
{
    UIWindow *window;
    UINavigationController *navController;
}
 
@property (nonatomic, retain) IBOutlet UIWindow *window;
 
@end

All we’re doing here is creating a UINavigationController object so that we can access it in our application delegate implementation file. Let’s go ahead and open up our ViewDemoAppDelegate.m and add the following code to the application did finish launching method.

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
	//Allocate and instatiate a new UINavigationController Object
	navController = [[UINavigationController alloc] init];
 
	//Allocate and initialize a First View Controller Object
	FirstViewController *firstViewController = [[FirstViewController alloc]init];
 
	//Push the first view controller onto the view controller stack without animation
	[navController pushViewController:firstViewController animated:NO];
 
	//Some memory management
	[firstViewController release];
 
	//Make it visible
	[window	addSubview:navController.view];
 
    // Override point for customization after application launch
    [window makeKeyAndVisible];
}

The first thing we’re doing in this function is instantiating the UINavigationController that we declared in our header. We pass it an alloc and init to allocate the appropriate amount of memory for it as well as an init to initialize it properly. The next thing we’re going to do is create a new FirstViewController object. Now you may be asking what a FirstViewController object is? That’s because we haven’t created it yet. So let’s go ahead and go to “File > New File” and add a new file that’s of the type UIViewController subclass. Make sure you check the little check box that says “With XIB for user interface”. This will give you an XIB file to work with instead of producing your interface programmatically. Now that we have our FirstViewControllerObject the only thing we have to do is import the header into the ViewDemoAppDelegate. So make sure to declare #import “FirstViewController.h” at the top of your code. Now onto the rest of the code in our application did finish launching method. The first thing we do with our allocated view controller is push it onto the navigation stack. Now because this is our first view we’re pushing onto the navigation stack we want to make sure to pass “NO” to the animated parameter because we don’t want to animate the root view when it’s pushed onto the stack. Now for some memory management. Once the FirstViewController is pushed onto the navigation stack the retain count on the object moves up to 2, therefore we can perform a release on the FirstViewController object so that the retain count stays at 1. Last thing we do in this method is add the view so our window so that it’s actually visible when we load the application.

Now we’re going to get into some interface builder stuff. Let’s start by opening up our FirstViewController.xib file in Interface Builder(or by just double clicking on it). You should see just a plain white view. Now in order to transition between views we need a button to press to actually transition between views. So let’s go ahead and add a UIButton to the view. Label the button anything you would like to to transition between views. Go ahead and save that file for now(we’ll come back to it later to connect the pointers). Next we’re going to create a new file in our project that’s a subclass of UIViewController. Again we’re going to make sure that “use XIB for user interface” is selected and press create. We’re going to name this view controller “SecondViewController” as it’s the one that we transition to from pressing the button on our first view controller. Once you create the new View Controller you should see it appear on the left hand side in your package explorer. Let’s go into our FirstViewController.h and add the following code.

#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
{
	IBOutlet UIButton *transitionButton;
}
 
- (IBAction) pushViewContoller:(id) send;
 
@end

All we do here is create an IBOutlet for the UIButton we added to the XIB file and call it transition button. Then we create an IBAction method that will get called whenever the user presses inside the UIButton. Let’s open up our FirstViewController.m implementation file and implement the IBAction method like so…

#import "FirstViewController.h"
#import "SecondViewController.h"
 
@implementation FirstViewController
 
- (IBAction) pushViewContoller:(id) send
{
	//Allocate and instantiate the second view controller object
	SecondViewController *secondView = [[SecondViewController alloc] init];
 
	//Push the secondViewController onto the navigation stack
	[self.navigationController pushViewController:secondView animated:YES];
 
	//Some memory management
	[secondView release];
}

Now as you can see, we first provide our implementation with an import of the SecondViewController.h that we created earlier. We then proceed to allocate and instantiate our SecondViewController object. This time we’re going to push it onto our navigation stack and passing the YES parameter to our animated option because this time we do want it to be be animated when we press the button. After that we just provide some basic memory management to bring the retain count of the SecondViewController object back down to 1. That’s al the necessary code we need right now, however if you run this program right now you’ll notice that if you press the button on the first view controller it won’t really do anything. So let’s open back up our FirstViewController.xib file and hook up our IBOutlet and IBAction to our button that we created. After you’re finished hooking up these actions in the File’s owner connections inspector, the file’s owner connections inspector should look something like this..

Now you should be able to run the application and press the button and see the transition to the second view controller(which will be blank unless you added something to the nib) and press the back button in the navigation controller and transition back to the first screen.

If you’re having some troubles, here is the project and all the code. Download

Also again, if you have any questions feel free to leave them in the comments section.


12 Responses to “Iphone Programming Part 2 – Intro to Views”

Leave a Reply