iPhone Application Developmen : Using the View-Based Application Template (part 3)

1/13/2011 11:16:31 AM

Implementing the View Controller Logic

With the view complete and the connection to the view controller in place, the only task left is to fill in the view controller logic. Let’s turn our attention back toward the HelloNounViewController.h and HelloNounViewController.m files. Why do we need to revisit the interface file (.h)? Because we need to easily access the userOutput and userInput variables, and to do that, we have to define these as properties, like this:

@property (retain, nonatomic) UITextField *userInput;
@property (retain, nonatomic) UILabel *userOutput;

Edit HelloNounViewController.h to include these lines after the @interface block. The finished file is shown in Listing 2.

Listing 2.
 #import <UIKit/UIKit.h>

@interface HelloNounViewController : UIViewController {
IBOutlet UILabel *userOutput;
IBOutlet UITextField *userInput;

@property (retain, nonatomic) UITextField *userInput;
@property (retain, nonatomic) UILabel *userOutput;



To access these properties conveniently, we must use @synthesize to create the getters/settings for each. Open the HelloNounViewController.m implementation file and add these lines immediately following the @implementation directive:

@synthesize userInput;
@synthesize userOutput;

This leaves us with the implementation of setOutput. The purpose of this method is to set the output label to the contents of the field that the user edited. How do we get/set these values? Simple! Both UILabel and UITextField have a property called text that contains their contents. By reading and writing to these properties, we can set userInput to userOutput in one easy step.

Edit HelloNounViewController.m to include this method definition, following the @synthesize directives:

-(IBAction) setOutput:(id)sender {

It all boils down to a single line! Thanks to our getters and setters, this single assignment statement does everything we need.

By the Way

Had we not used @synthesize to create the accessors, we could have implemented the setOutput logic like this:

[userOutput setText: [userInput text]];

Either way is fine technically, but you should always code for readability and ease of maintenance.

Freeing Up Memory

Whenever we’ve used an object and are done with it, we need to release it so that the memory can be freed and reused. Even though this application needs the label and text field objects (userOutput and userInput), as long as it is running, it is still good practice to release them in the dealloc method of the view controller. The release method is called like this:

[<my Object> release]

Edit the HelloNounViewController.m file’s dealloc method to release both userOutput and userInput. The result should look like this:

- (void)dealloc {
[userInput release];
[userOutput release];
[super dealloc];

Well done! You’ve written your first iPhone application!

Building the Application

The app is ready to build and test. If you’d like to deploy to your iPhone, be sure it is connected and ready to go, and then choose iPhone Device from the drop-down menu in the upper left of the Xcode window. Otherwise, choose Simulator. Click Build and Run.

After a few seconds, the application will start on your iPhone or within the simulator window, as shown in Figure 14.

Figure 14. Your finished application makes use of a view to handle the UI and a view controller to implement the functional logic.

  •  iPhone Application Developmen : Using the View-Based Application Template (part 2) - Preparing the View Controller Outlets and Actions
  •  iPhone Application Developmen : Using the View-Based Application Template (part 1)
  •  Mobile Application Security: Application Format
  •  Mobile Application Security: Security Testing
  •  Mobile Application Security: The Apple iPhone - Development
  •  Building Android Apps : Installing KiloGap in the Emulator
  •  Building Android Apps : Build KiloGap
  •  Building Android Apps: Create an Android Virtual Device
  •  Building Android Apps: Going Native - Setting Up the Environment
  •  Building Android Apps: Introduction to PhoneGap
  •  iPhone Application Development : How Xcode and Interface Builder Implement MVC
  •  iPhone Application Development : Understanding the Model-View-Controller Paradigm
  •  Building Android Apps: Going Offline - Debugging
  •  Building Android Apps: Creating a Dynamic Manifest File
  •  Building Android Apps: Online Whitelist and Fallback Options
  •  The Basics of the Offline Application Cache
  •  Building Android Apps: Web SQL Database (part 4) - Deleting Rows
  •  Building Android Apps: Web SQL Database (part 3) - Selecting Rows and Handling Result Sets
  •  Building Android Apps: Web SQL Database (part 2) - Inserting Rows
  •  Building Android Apps: Web SQL Database (part 1) - Creating a Database
    Top 10
    Programming .NET Security : Extending the .NET Framework (part 2) - Defining the Key Exchange Deformatter
    Mobile Application Security: Security Testing
    The Art of SEO : How Links Influence Search Engine Rankings (part 2) - Additional Factors That Influence Link Value
    Windows Azure : Static reference data (part 2) - Performance disadvantages of a chatty interface & Caching static data
    Exchange Server 2010 : Administering Mailbox Content - Monitor and Restrict Communication (part 2) - Apply Common Monitoring and Restriction Scenarios
    Windows 7 : Protecting Your Computer While Browsing (part 4) - Restricting Permissions Using Security Zones
    OData with SQL Azure - OData Overview
    Exchange Server 2010 : Meet Message Retention Compliance (part 2) - Archive Email
    iPhone 3D Programming : Textures and Image Capture - Dealing with Size Constraints
    Windows Server 2008 : Designing Organizational Unit and Group Structure - Group Policies and OU Design
    Most View
    The .NET Security Architecture
    Windows 7 : Managing Other People’s User Accounts (part 1)
    Windows 7 : Using Windows Defender (part 2) - Scanning Your Computer for Spyware and Malware
    Using MySQL Enterprise (part 3) - Query Analyzer
    Upgrading to Windows Server 2003 : Planning a Windows NT Domain Upgrade (part 1)
    Deploying a Public Key Infrastructure with Windows Server 2008 R2
    iPhone 3D Programming : Adding Depth and Realism - Lighting Up (part 2)
    Exchange Server 2010 : Maintaining Reliability and Availability - Recover Data
    Externalizing BLOB Storage in SharePoint 2010 (part 1)
    Use the Command Line with IIS 7.0
    Understanding and Using Windows Server 2008 R2 UNIX Integration Components (part 1)
    Using and Configuring Public Folder Sharing
    ASP.NET State Management Techniques : The Role of the Global.asax File
    Algorithms for Compiler Design: PEEPHOLE OPTIMIZATION
    .NET Components : Configuring Permissions
    Getting Familiar with AJAX
    Enterprise Patterns with WCF RIA Services
    Windows 7 : Using Compression and Encryption (part 2) - Compressing Files and Folders
    Using Windows Phone 7 Technologies : Understanding Orientation and Movement
    Algorithms for Compiler Design: EQUIVALENCE OF TWO AUTOMATAS