Cocoa Touch Tutorial: iPhone Application Example
Similar to one of my first blog posts on building a basic application for Mac OS X using xcode 3.0, I am going to explain for beginning iPhone/iPod Touch developers how to build the most basic Cocoa Touch application using Interface Builder and an application delegate in xcode 3.1. This tutorial post is really to provide a quick how-to. I won’t go into any depth explaining why things are done the way they are done, but this should help you get up and running with your first application pretty quickly so that you too can clog the App Store with useless superfluous apps (kidding… just kidding).
If you are a visual learner, it may be helpful to you to instead watch a video presentation of this tutorial. I’ve posted it on the site, but you’ll have to click the link to see my Cocoa Touch Video Tutorial. Understanding Cocoa programming is much simpler if you learn MVC, Model, View, Controller. You can probably step through code examples and figure some things out without learning MVC, but I wouldn’t recommend it. Go Google it and read up on it.
I will say as an introduction to MVC for those who are not familiar that it should probably be called (Model <--> Controller <--> View) or (View <--> Controller <--> Model) as the controller always sits between the other two. Your controller is either telling your model to update its data or it is telling the view to update its display. That’s the crux of the whole paradigm. The details run much deeper, but that’s how I will nutshell it for you.
Create Your Application
Let’s get started. Create a Cocoa Application using the following steps:
- Select File > New Project…, under the iPhone OS templates choose Window-Based Application in the ensuing dialog. Click Choose…
- Enter ‘Basic iPhone App’ as the project name. Click Save
You should see a project workspace like the following:
The next thing you should do is create a class to act as your controller or delegate.
Delegate == Controller
The words delegate and controller can be used synonymously. You’ll see later that we delegate the work of the different controls we create in Interface Builder to a delegate or controller class. In the iPhone template projects, this application delegate is created for you. Our app delegate has been named Basic_iPhone_AppAppDelegate.
In our app delegate class we need to add what Cocoa developers refer to as outlets and actions. I could spend an entire post explaining these two things in depth, but for the sake of brevity and walking you through the steps to build your first application, the definition will have to suffice.
Outlets represent controls in your user interface that can have some action performed upon them. Action are functions in your code that are connected to controls in your user interface such as a button or a drop down list. When connected to a button for instance, the action code will be run when the user clicks the button.
In xcode, open your app delegate header file Basic_iPhone_AppAppDelegate.h. Add an outlet for the text field and the label below the window outlet as in the following snippet:
1 2 3 4 5 | @interface Basic_iPhone_AppAppDelegate : NSObject <UIApplicationDelegate> { IBOutlet UIWindow *window; IBOutlet UITextField *textField; IBOutlet UILabel *label; } |
You will also want to add an action that will be performed when our button is clicked. Add that below the property for our window:
1 2 3 4 5 6 7 8 9 | @interface Basic_iPhone_AppAppDelegate : NSObject <UIApplicationDelegate> { IBOutlet UIWindow *window; IBOutlet UITextField *textField; IBOutlet UILabel *label; } @property (nonatomic, retain) UIWindow *window; - (IBAction)click:(id)sender; |
1 2 3 4 5 6 7 8 9 10 | - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after app launch [window makeKeyAndVisible]; } - (IBAction)click:(id)sender; { } |
We will actually add some code to do something in the click: action handler, but first we need to hook it up to the user interface in Interface Builder.
Interface Builder And Controller/Delegate Implementation
Now that you’ve specified the outlets–a UITextField and a UILabel and an action called click:, you will see these items available for connecting to the UI in Interface builder. Let’s open Interface Builder and make the connections we need using the following steps:
- In your xcode workspace, expand the folder in the tree view called Resources and double click the file called ‘MainMenu.xib’.
Note:: A .xib is a .nib that uses XML for the internal data structure.
This will open the xib file in Interface Builder
- Once Interface Builder loads, you will notice that there is an object representation of our app delegate in the MainWindow.xib window. This is the object you will use to create connections for your actions and outlets.
Design The User Interface
Now you need to add the controls to the main window in Interface Builder and then we can connect the action and outlet accordingly. To finish the interface, complete the following steps:
- Make sure that the Object Library is open by selecting Tools | Library from the menu in Interface Builder.
- Drag a TextField, a Label, and a Button to the main window so that the user interface looks like the screenshot below:
- Control-Click and drag from the Button to your app delegate object in the ‘MainWindow.xib’ window.
A pop-up will display. Select click:
- Control-Click the app delegate object and drag it to the text field in the main window.
A pop-up will display. Select textField
- Control-Click the app delegate object and drag it to the label in the main window.
A pop-up will display. Select label
That’s it for Interface Builder. You can quit interface builder and return to xcode. We have one more piece of code to add and then our application will be finished.
Finishing Up
When the button is clicked, it will simply grab the text from the text field and place it into the label. That’s all the application does. Here’s the code you need. Just make your implementation of the click: action in the Basic_iPhone_AppAppDelegate.m file look like this:
1 2 3 4 | - (IBAction)click:(id)sender; { [label setText:[textField text]]; } |
Notice we are grabbing the text from the text field, and setting the text in our label with it. Now all you need to do is click “Build and Go”. When the application runs the iPhone Simulator will be started. You will see the application load. Type some text into the text field and click the Change button. You will see the label update with the text from the text field.
Next Steps
This tutorial has just scratched the surface. It’s is intended to get you going quickly. For a more in-depth path to gaining a deeper understanding of iPhone development, take a look at Apple’s, Your First iPhone Application. You will need to log in with your ADC account to see the article, but it is definitely worth while.
Conclusion
The limit with iPhone development is really just your imagination. It is an extremely fun platform to develop on and the resulting applications are very rewarding. Have fun with it and learn as much as you can. Just do me a favor and don’t clog the App Store with any more flashlight apps or tip calculators… Seriously ;-) . Until next time.

Basic iPhone Application Demo Project


helpful post, keep posting on Cocoa Touch & Xcode codefrux http://www.codefrux.com
[...] Cocoa Touch Tutorial: iPhone Application Example – Explaining for beginning iPhone/iPod Touch developers how to build the most basic Cocoa Touch application using Interface Builder and an application delegate in xcode 3.1 [...]
[...] the label onto the main screen of the iphone and more. It’s about a 16 minute tutorial. Cocoa Touch Tutorial: iPhone Application Example – Explaining for beginning iPhone/iPod Touch developers how to build the most basic Cocoa [...]
This is an awesome tutorial. I read so many blogs to get started but all of them lacked proper explanation.. this one really cleared all my doubts..