BNR Day 4: Whale Watching

One camper (or is it rancher?) observed today that life at the ranch has settled into a routine like a cruise ship: sitting, eating, sightseeing exotic places and code, repeat.  For the rest of the week, the class will transition from learning Objective-C to harnessing its unique features to build iOS applications.  We’ll be following the Big Nerd Ranch iOS Programming book under the tutelage of Scott Ritchie, former NeXT Step and Apple employee, now acting as our captain to steer this cruise ship out to the ocean of iPhone and iPad app development.

The UIKit framework in iOS utilizes a Model-View-Controller architecture for handling data, drawing that data, and receiving events from user input.  Today’s topics highlighted the lifecycle of the controller and views, then the myriad of techniques that exist to hook up between the View and the Controller.  There are similarities for the adventurous .Net programmer struggling for a foothold.

Both frameworks implement a method-based callback method for receiving simple UI events.  In .Net it’s referred to as the Events & Delegates system.  The analogous pattern in Objective-C is called Target-Action.  The controller passes to the caller a receiver object and the selector (method name and a description of its arguments in a compact syntax) of the desired method to be called on event firing.  The XCode interface builder can be used to make these connections, but here is equivalent code performing this in each language:

// C#
.eventName += receivingObject.methodNameOnReceiver;

// Objective-C
[firingObject addTarget:receivingObject action:@selector(methodNameOnReceiver:) forControlEvents:EventName];

 In addition, Objective-C has a special implementation of the observer OO design pattern it calls “Delegation”.  By implementing a protocol (Interface in C#) in the controller and passing a reference to the calling object, a set of instance methods can be called on an object; this pattern is utilized by complex features like GPS.

ASP.Net practitioners should be familiar with the life-cycle of a code-behind class.  When receiving a request, the page will go through being created, loaded with ViewState, and custom control events, with numerous events (or overridden instance methods) fired at each step in the process.  These events run in sequence for each initial load and postback of an .aspx page. 

In the case of an iOS app, there is no constant cycle of destruction during normal operation.  The creation process happens only once then the event-loop takes over.  There is a partial destruction and restoration process when an app moves from the foreground to background in the system, but we’ve yet to cover that.

When conceptualizing an iOS controller and view’s lifecycle versus an ASP.Net page, there are analogous components and steps.  The Controller in UIKit is a lot like the code-behind class in ASP.Net.  Where .Net uses the .designer.cs (or .designer.vb) file to describe the fields of a class in code, XCode utilizes a .xib file, which is an XML-based description of the interface and its connections to fields and instance methods in the owning controller class.  Here are the generic steps with corresponding actions in each framework:

UI Lifecycle

ASP.Net Code-Behind

UIKit Controller

Object and direct children created, but system doesn’t have full context yet

Constructor  in C#

new in VB.Net


Init (OnInit method)

init instance method

Runtime steps in to load UI, providing crucial context for event coordination later

ViewState loaded by runtime (NOTE: Member controls are created during construction)

.xib file loaded, instantiating the View and readying its connections make in Interface Builder.

With full context restored, instance methods can manipulate the object and its children, performing additional setup

Load (OnLoad method)


System enters event-loop, receiving user events and routing them to listeners setup during creation


Events & Delegates


Methods previously passed as event handlers during load in C#


Instance methods marked with “handles” and an event list in VB.Net

Instance methods called through connection in the .xib file using the Target-Action pattern.


Instance methods called by implementing a Protocol and assigning self as a Delegate (Observer Pattern)

This comparison hopefully throws a life-ring to any struggling .Net programmers who find themselves in the deep ocean of Objective-C and UIKit.  It is important to remember that each framework has a disparate set of features with some overlap.  Look for similarity, but expect and appreciate differences.  Even atop this cruise ship, where we’re sometimes only getting a high-level view through example, we’re always looking to sort out the whales from the Loch Ness Monster.