{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
View Controller for the First iPhone Application Explained
Summary
This article explains the view controller of an iPhone application. In this case, the view contains just a table and hence the controller is derived from UITableViewController class. Class diagram, general view methods, and general table methods are discussed.
 
Table of Contents

Creation of the first iPhone application

Source Files

The Class Diagram

Figure 1. The class diagram for a view controller that is derived from the table view controller.

The Header File

Code Listing 1. The Root View Controller Declaration

The Implementation File

Code Listing 2. The implementation file for the root view controller

Standard View Controller Methods

shouldAutorotateToInterfaceOrientation

Code Listing 3. The shouldAutorotateToInterfaceOrientation method

Figure 2. The callstack leading to shouldAutoRotateToInterfaceOrientation

Figure 3. The user interface has not been rotated automatically when the iPhone is rotated. This is because the method shouldAutorotateToInterfaceOrientation returned NO.

Code Listing 4. Automatically rotating the user interface in an iPhone application

Figure 4. The user interface has been rotated automatically when the iPhone is rotated. This is because the method shouldAutorotateToInterfaceOrientation returned YES.

didReceiveMemoryWarning

Code Listing 5. The didReceiveMemoryWarning method

Figure 5. The callstack leading up to didReceiveMemoryWarning

dealloc

Code Listing 6. The dealloc method

Table View Controller Specific Methods

Code Listing 7. The declaration of UITableViewController

Figure 6. The connections panel of the table view

numberofSectionsInTableView

Code Listing 8. The numberOfSectionsInTableView method

numberOfRowsInSection

Code Listing 9. The numberOfRowsInSection method

cellForRowAtIndexPath

Code Listing 10. The cellForRowAtIndexPath method

didSelectRowAtIndexPath

Code Listing 11. The didSelectRowAtIndexPath method

 
Article Series
Previous Article:
Test Your Knowledge: Application Delegate for the First iPhone Application Explained
This article is part of the Series:
Article Series: Getting started with iPhone development
Next Article:
Test Your Knowledge: View Controller for the First iPhone Application Explained

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.

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 doesn’t have a superview
    // Release anything that’s 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

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.

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.

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 doesn’t have a superview
    // Release anything that’s 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

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

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
}

Take a Quick Quiz on this Article

1. Which method would you use to automatically rotate the user interface?



: Rotate the UI
Question 1 of 5
Article Series
Previous Article:
Test Your Knowledge: Application Delegate for the First iPhone Application Explained
This article is part of the Series:
Article Series: Getting started with iPhone development
Next Article:
Test Your Knowledge: View Controller for the First iPhone Application Explained
Bookmark and Share This

More Articles With Similar Tags
This is a test on the article that gives a quick overview of the development tools installed with iPhone SDK. These tools include Xcode, Interface Builder, Instruments, and iPhone Simulator. In the article, Dash Code, which is used to build web applications for iPhone is also introduced. GDB, the open source debugger, is discussed there as well.
icon-xcode-groups-files.jpg
This article discusses various parts of the Project window inside an Xcode project. It also gives a quick overview of the Groups and Files navigation window. The Run menu, the default toolbar, and the built-in source editor are also discussed.
This is a test on the article that discusses various parts of the Project window inside an Xcode project. It also gives a quick overview of the Groups and Files navigation window. The Run menu, the default toolbar, and the built-in source editor are also discussed.
icon-xcode-interface-builder-connection-panel.jpg
This article explains the user interface files of an iPhone application. Explains the contents of the .xib files, which store the user interface. Goes through the various windows (Document Window, User Interface Window, Inspector Window, Library Window, Connections Panel) of the Interface Builder. The example used is a simple navigation-based iPhone application.
This is a test on the article that explains the user interface files of an iPhone application. It explains the contents of the .xib files, which store the user interface. Goes through the various windows (Document Window, User Interface Window, Inspector Window, Library Window, Connections Panel) of the Interface Builder. The example used is a simple navigation-based iPhone application.
About  Contact  Privacy Policy  Site Map