iPhone Development 101

iPhone 101

Code Tips

Resources

Links

Subscribe

TwitterRSSE-mail

More

Learn iPhone Programming:
Putting it Together: Building the First Project

This tutorial was written for Xcode 3.2; it's outdated now. For a more up-to-date tutorial, check out Apple's App Development Tutorial to learn how to write iOS apps. There are also lots of other excellent books and online tutorials you can learn from.

I'll still be posting code tips and shortcuts to idev101; click here for the index. If you'd like to get updates, follow me on Twitter, or by RSS or E-mail.

At this point your First project is almost done. Your header FirstAppDelegate.h file should look like this:

#import <UIKit/UIKit.h>

@interface FirstAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    IBOutlet UIButton *aButton;
    IBOutlet UILabel *helloLabel;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

- (IBAction)pressButton:(id)sender;

@end

In Interface Builder you've added a UIButton and a UILabel to your window, and made the connections for them. You can double-check the connections by control-clicking on the orange object icon to bring up the connections panel:

Your IBOutlet variables should appear under "Outlets", and the objects they're connected to are listed on the right. Your IBAction method appears under "Received Actions".

Now you just need to implement the pressButton method in your implementation (.m) file. Click on FirstAppDelegate.m in the Groups & Files section in Xcode. Leave the existing methods there, and add a new one for pressButton. To avoid typos, you may want to copy-paste the method declaration from the header file, then change the closing semicolon to braces:

- (IBAction)pressButton:(id)sender {
}

Your code goes between the braces. For this first project, let's have the button-press do two things:

  • write a log message to the console, and
  • change the text of the label.

To write a log message, you'll use NSLog. The syntax is NSLog(@"my message here"). NSLog is actually a C function, but it takes an NSString argument. When you use a quoted NSString in your code, you must prefix the quotes with an @-sign: @"this is a string".

Add the NSLog function call to your method:

- (IBAction)pressButton:(id)sender {
    NSLog(@"button was pressed!");
}

Next you'll need to change the text of the label, so it says something more interesting than just "Label". You declared the helloLabel instance variable in the .h file; this variable represents your UILabel object. Since helloLabel is an instance of the UILabel class, you can call any instance methods on that class, and view or set any of the properties.

Open up the Xcode documentation window and find the class reference for UILabel. (Remember you can option-double-click on the UILabel name in your .h file.) Look at the properties for UILabel. Which property do you think controls the text that appears on the label?

Properties can be set using the method setPropertyName:newValue. Since the text property is an NSString, you set it like this:

[helloLabel setText:@"Hello, world!"];

Note that even though the property name ("text") is lowercase, the "setPropertyName" method capitalizes it.

Here's what your pressButton method should look like now:

- (IBAction)pressButton:(id)sender {
    NSLog(@"button was pressed!");
    [helloLabel setText:@"Hello, world!"];
}

Now you're ready to compile the project. Look at your Xcode toolbar and be sure you have the "Active SDK" set to the Simulator. To compile without running the project, click on the Build icon.

If you set the build preferences in Xcode to always open the build results window, then a new window will pop open after you click "Build". A successful build looks like this:

If you have any errors in your code, they'll show up in the build results window. For example, if you forget to put the semicolon on the end of your setText line, you'll get an error. The build results will usually show you which file/line the error occurred on. Example

Once you've corrected any errors, you can click the "Build and Run" icon:

This compiles your project and, if the compile is successful, launches the app in the iPhone simulator.

If you set up your debugging preferences in Xcode to show the console on start, then you'll also see the console window when you run your app in the simulator:

Press the button a few times and notice how your NSLog statements appear on the console. The console will be very useful when you're debugging larger projects.

Source Code

First.zip

TopHomeContents