Home / iPad

Figuring Out Where Your Code Goes

One of the biggest challenges facing a developer working with an unfamiliar framework and template is figuring out where in the control flow - the sequence in which messages are sent during execution - to put the code to get something done.

The delegate object

Okay, here's how the template set up the DeepThoughts app:

  1. UIApplication loads the parts of the MainWindow.xib to create DeepThoughtsAppDelegate and the window.
  2. UIApplication sends DeepThoughtsAppDelegate the application: didFinishLaunchingWithOptions: message.
  3. The application:didFinishLaunchingWithOptions: method in DeepThoughtsAppDelegate initializes the view (viewController).

Note that the application:didFinishLaunchingWithOptions: message is sent at the very beginning, before the user can even see anything on the screen. Here's where you'd insert your code to initialize your application - where you'd load data, for example, or restore the state of the application to where it was the last time the user exited. (For DeepThoughts, you don't need to change anything here - it's all supplied by the template.)

The view controller object

DeepThoughtsViewController is the controller responsible for managing the view. Select DeepThoughtsViewController.m to see its code in the Text editor. Then click the Methods list pop-up menu in the Xcode Text Editor's Navigation bar (to the left of the Bookmarks menu), to see the controller object's methods.

What you see in the pop-up menu is a list of active methods in the controller object.

You can also see that the code in the Text editor starts off with sections of comments that include code you can implement if you want to, simply by removing the comments. (As Objective-C programmers already know, the lines beginning with // are single-line comments that don't do anything. A line beginning with /* followed by a line ending with */ marks an entire comment section that doesn't do anything.)

The first of these (commented out) sections starts off with The designated initializer, the second with Implement loadView, and the third with Implement viewDidLoad.

After those comment sections, you encounter the first active method:

- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return YES;

This method (which appears as shouldAutorotateToInterface Orientation: in the Methods list pop-up) starts your app with the default portrait orientation, unless you override it. The comment above this code says "Override to allow orientations other than the default portrait orientation."

For DeepThoughts, you don't have to do anything with this code because you want the app to start off in portrait orientation, but for other apps you may create, you can override the code by simply adding comment markers to comment it out (make the code inactive). To add comment markers, insert /* before the beginning of the statement (right before - (BOOL)), and insert */ on a line after the last bracket (}).

Marking code sections in the view controller

Before adding your code to DeepThoughtsViewController.m, it helps to know what each section of the template-provided code does and to mark off each section so that you can navigate quickly to the sections you're interested in. You can do so by marking each of the code territories.

The # pragma mark statement marks each territory, and the marks themselves appear in the Methods list pop-up menu. You use the # pragma mark statement with a label (such as View life cycle or Orientation) to add the label in bold to the Methods list so that you can logically identify and keep separate the methods in the list.

[Previous] [Contents] [Next]