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;
-(IBAction)setOutput:(id)sender;
@end
|
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 {
userOutput.text=userInput.text;
}
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:
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.