Creation of the first iPhone application
The first iPhone application discussed in this article has been created in the following article:
Creation of First iPhone application explained
This project is a simple navigation-based application. It uses a table view on the main window. From the code side, a table view controller (UITableViewController) is used to provide the functionality for a table based view.
The article below discusses the source code of the main view controller.
Source Files
The view controller for the main view is created in the following Objective-C files:
- RootViewController.h
- RootViewController.m
The Class Diagram
Figure 1. The class diagram for a view controller that is derived from the table view controller.

Here the RootViewController class is derived from the UITableViewController class. The methods from the RootViewController class can be divided into two groups:
- General View Controller Methods
- Table View Controller Methods
The general view controller methods include the following:
- shouldAutoRotateToInterfaceOrientation
- didReceiveMemoryWarning
- dealloc
The table view controller methods include the following:
- numberOfSectionInTableView
- numberOfRowsInSection
- cellForRowAtIndexPath
- didSelectRowAtIndexPath
The Header File
Following is the header file where the view controller is declared:
Code Listing 1. The Root View Controller Declaration
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController {
}
@end
As you can see, the base class for RootViewController is UITableViewController.
The Implementation File
Following is the implementation file for the main view controller:
Code Listing 2. The implementation file for the root view controller
//
// RootViewController.m
// FirstApp
//
#import "RootViewController.h"
#import "FirstAppAppDelegate.h"
@implementation RootViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesnt have a superview
// Release anything thats not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
@end
Standard View Controller Methods
This class implements some methods that are part of the regular view controller (UIViewController):
- shouldAutorotateToInterfaceOrientation
- didReceiveMemoryWarning
- dealloc
The method dealloc, of course, is used in all the objects (part of the root class NSObject).
shouldAutorotateToInterfaceOrientation
Whenever the iPhone is rotated, this method shouldAutorotateToInterfaceOrientation is called.
Code Listing 3. The shouldAutorotateToInterfaceOrientation method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
In the picture below, you can see that the iPhone is turned to landscape mode (interfaceOrientation is UIInterfaceOrientationLandscapeLeft).
Figure 2. The callstack leading to shouldAutoRotateToInterfaceOrientation

However, you can see in the code that we are returning FALSE (as interfaceOrientation is not equal to UIInterfaceOrientationPortrait -- i.e. it’s now in the landscape mode, not portrait mode). So, the view will NOT be automatically rotated (as we are returning FALSE from this method).
Figure 3. The user interface has not been rotated automatically when the iPhone is rotated. This is because the method shouldAutorotateToInterfaceOrientation returned NO.

So, you can see in the figure above the table view has not been rotated. This is probably the right thing to do in most of the case, because you would typically want more items listed in a table.
Code Listing 4. Automatically rotating the user interface in an iPhone application
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
// return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}
However, if we return YES from the shoudlAutorotateToInterfaceOrientation method, the view will be automatically changed to fit the landscape mode, as shown in the figure below. This might not be what you want for a lot of applications.
Figure 4. The user interface has been rotated automatically when the iPhone is rotated. This is because the method shouldAutorotateToInterfaceOrientation returned YES.

didReceiveMemoryWarning
If a memory warning is received (for example, if your application is continually saving some new data - like some audio), then that activity requiring lot of memory might need to be stopped.
Code Listing 5. The didReceiveMemoryWarning method
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesnt have a superview
// Release anything thats not essential, such as cached data
}
You can simulate a memory warning from the iPhone simulator (from Hardware - Simulate Memory Warning menu item).
Figure 5. The callstack leading up to didReceiveMemoryWarning

dealloc
Code Listing 6. The dealloc method
- (void)dealloc {
[super dealloc];
}
In this view controller, no new objects were created. So, there is no need to release any of those objects. However, dealloc method from the base class is called.
Table View Controller Specific Methods
This view controller is derived from UITableViewController. There is a table in the view. So, we would need to implement some of the methods for the table view.
The three methods listed below are from the UITableViewDataSource protocol. If you look at the declaration of UITableViewController, you can see that it implements two protocols UITableViewDelegate and UITableViewDataSource.
Code Listing 7. The declaration of UITableViewController
UIKIT_EXTERN_CLASS @interface UITableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
@private
UITableViewStyle _tableViewStyle;
void *_reserved;
}
If you look at the connections panel of the table view (of class UITableView), you can see two outlets: dataSource and delegate.
Figure 6. The connections panel of the table view

In this case, both the dataSource and delegate are set to File’s Owner. The class behind the File’s Owner is RootViewController - the class that you wrote (which is derived from the UITableViewController which implements the table view delegate and datasource protocols.
numberofSectionsInTableView
There can be multiple sections in a table view.
Code Listing 8. The numberOfSectionsInTableView method
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
Here the entire table is just one section.
numberOfRowsInSection
How many rows are there in each section.
Code Listing 9. The numberOfRowsInSection method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
cellForRowAtIndexPath
Used for drawing the individual cell.
Code Listing 10. The cellForRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell
return cell;
}
didSelectRowAtIndexPath
A particular row has been selected. This method is part of UITableViewDelegate protocol.
Code Listing 11. The didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
}