Beginning iOS Programming

I recently decided to put aside my differences with Apple and learn the platform.  I figure knowing Android, Windows Phone 7, and iOS is a very marketable set of skills.  For a long time I have made attempts to get into iOS but I always stumbled when I hit Objective-C.  I tried going the MonoTouch route, but I don’t really feel you can fully understand MonoTouch without understanding iOS and, by extension, Objective-C.  So with Pluralsight license in hand I decided to make a concerted effort to learn Objective-C and begin to grasp the principle of iOS programming.

After a full day of watching the Pluralsight “Introduction to iOS – Part 1” video I am amazed at how far I am come.  I decided to work up a quick Twitter Profile app.  While I am still hung up on the network communication aspect of iOS, I think I have grasped, quite well, the understanding of how to interact with UI components through Outlets and Delegates.

Understand how it works

The best way I can relate how iOS works to myself is to compare it to the MVP model of ASP .NET, with the code behind.  iOS claims to feature the MVC pattern, however, this really does seem to a big misnomer, the xib files are hardly passive and seem to be linked to a single controller (ala presenter).  Within iOS, the responsibility of responding to and communicating with the View (xib files) lies with a “view controller” which is the Presenter (not a controller).  This line of code, from the didFinishLaunchingWithOptions in the AppDelegate.m file, appears to specify this relationship:

self.viewController = [[[ViewController alloc] initWithNibName:@”ViewController” bundle:nil] autorelease];

Notice that the file extension for my xib file (ViewController.xib) is not specified.  autorelease is new in iOS as a way to handle garbage collection.

Wireup the UI controls

Placing the UI controls using Interface Builder is simple.  For my application, I am using 8 labels, a header bear, and a search bar.  This is the UI as it appears in Interface Builder:


There are actually labels here to hold the content as it comes back.  To wire these controls up, we need to add them as properties to the ViewController.  This is similar to what happens in the code behind for .NET applications, it just happens automatically.  Here are my definitions for the controls:

@property (retain, nonatomic) IBOutlet UILabel *lblTwitterUser;

@property (retain, nonatomic) IBOutlet UILabel *lblFollowersCount;

@property (retain, nonatomic) IBOutlet UILabel *lblFriendsCount;

@property (retain, nonatomic) IBOutlet UILabel *lblTweetCount;

@property (retain, nonatomic) IBOutlet UILabel *lblCurrentStatus;

IBOutlet “marks” properties as being available to controls in the UI (View).  The @property modifier indicates that this variable will be exposed as a property.  These are similar to the C# variety, but we just don’t explicitly implement getter and setters, these are created using the @synthesize modifier. Once you have made these definitions you can perform the wire up; it must be done physically.

Implementing Search

You may have noticed that we have not talked much about the search bar, nor have we defined an IBOutlet for it.  This is because search is handled using a delegate.  A delegate (also called a protocol) indicates a method that will handle an “event”.  Think of this as an interface call.  When the “event” happens, it looks to see if the controller implements the delegate (though implementation is optional).  In this case we will implement the UISearchBarDelegate, this is defined for usage in the .h file as such:

@interface ViewController : UIViewController<UISearchBarDelegate> {



The method we want to implement in the .m file is searchBarSearchButtonClicked.  I have implemented as such:

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {

// get the provided user name

NSString *handleName = searchBar.text;

TwitterProfile *profile = [TwitterProfile performLookup:handleName];


// assign the values

lblCurrentStatus.text = profile.currentStatus;

lblTwitterUser.text = handleName; //profile.username;

lblTweetCount.text = [NSString stringWithFormat:@”%d”, profile.tweetCount];

lblFollowersCount.text = [NSString stringWithFormat:@”%d”, profile.followersCount];

lblFriendsCount.text = [NSString stringWithFormat:@”%d”, profile.friendsCount];


[searchBar resignFirstResponder];


Now, don’t make the same mistake that I did here.  There is another step to making this work.  Open the Interface Builder application and set the delegate to the “File’s Owner (the ViewController in this case)”.  Below you see the end result:


If you forget to do this, the event will not be caught by the Controller.

Closing Thoughts

Objective-C is certainly different from any of the languages I have worked with in the past, but I can see its draw.  It has kind of a C++ type feel to it and you really can leverage a lot of power.  Once you get passed the “message passing” idiom its just like any other language.  I am actually, now, looking forward to learning more about the offerings of iOS.  Who knows, I might even submit a personal app one day :0

Up next, I need to figure out how network communication is done in Objective-C so I can actually make this application read from the Twitter REST API instead of simply hardcoding the result.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s