Learning iPhone programming – Update 1

Where have I been? What have I been doing? iPhone programming! My previous update on Android programming, from which I only toyed around with, is a lot different than this. Rather than just evaluate the software and programming experience, I have taken the full plunge into iPhone programming. I got a shiny new 21.5″ iMac, a book on Objective-C, and (of course) Xcode! The iMac isn’t just for me, nor was it bought primarily for iPhone programming. It is the new family computer, and I just had some persuasion on getting an iMac instead of a Windows machine. You will notice that this is “Update 1″, assuming that there is going to be more. This is because I plan to post more updates once I subscribe to the Apple developer program, and when I release some apps. Considering that the Apple developer fees ($100 a year!!) are coming out of my personal budget, I want to actually get an app made before I go and buy into the program. Until then, I can’t put apps on my device or submit them to the app store.

I wanted to do a review on the iMac, but I’ll save that for another post as it’s a little off topic now. This review is about Xcode and Objective-C. Lets get into it now! Objective-C is one of the strangest languages I’ve ever programmed in. Aside from objects, it uses a total of 4 programs, 2 of which have to be manually edited every time you add a user interface. It has a Delegate header file, a Delegate implementation file, a Controller header file, and a Controller implementation file. The Controller implementation file is the one where most of the main code takes place, as it provides access to methods that are related to the user interface. However, you have to take care of the UI also in the Controller header file, by declaring and setting the variables as properties that are passed on from the Interface Builder. Lost me? The Interface Builder is a completely separate program that edits the .xml file that the user interacts with. It’s the most user-friendly part of the whole setup, but is still complicated in contrast with the interface builder in Visual Basic 2010. After placing your buttons, labels, or anything else you want to interact with in some way, you have to connect it to the main file by holding “Control” and dragging a line from the item to the File Builder logo in one of the 4 windows the program uses. You then select a variable to connect to, and presto! You have a linked UI! At first I found this exceptionally complicated, but after a week of working with it (for that is how long I have had the computer to this date) I have grown to appreciate it.

To give you an accurate example of iPhone programming (of what I’ve learned so far, anyway) here is a little walkthrough of how to make a program that let’s you push a button to make it tell you “Hello”.

Open a new project in Xcode, select “View-Based Template” and give it a name.

First, update the Controller header file (.h) with the following code in the implementation loop:
IBOutlet UILabel *outputLabel;
Then this outside of the loop before the “@end” statement.

@property (retain, nonatomic) UILabel *outputLabel;


Now, save your files and open up Interface Builder by double clicking the “ Interface.xml” file and add a new button and Label. Link them to the “outputLabel” and “activateHello” controllers (a.k.a variables) that we defined earlier by Control-dragging lines from the File Owners icon to the item. Select the appropriate controller when prompted, then save and close interface builder. Go back to Xcode, edit the Controller Implementation file with the following code under the header-code right before the first comment:

@synthesize outputLabel;

-(IBAction)activateHello:(id)sender {
NSString *helloString;

helloString = [[NSString alloc] initWithString:
@"Hello there"];
[outputLabel.text = helloString.text];

That’s it.
There you go, a quick little runthrough on iPhone programming. The text beneath the little dashed line was written 31 days after the original article. Why? Upon visiting my site I realized I had forgotten to push the “Publish” button so this draft had been sitting in my browser the whole time. Nice.
Well at least you will get another article soon.

Hope you enjoyed it!

This entry was posted in Experiments, Observations, Updates, Works in Progress. Bookmark the permalink.

Leave a Reply