Using the controls you currently know, there are
probably half a dozen different ways that you might imagine creating a
date entry screen on the iPhone. Buttons, segmented controls, text
fields—all of these are potential possibilities, but none has the
elegance and the inherent usability of a date picker. Let’s put the
picker to use.
Implementation Overview
This project, which
we’ll be calling DateCalc, will make use of a date picker (UIDatePicker) that, when set, will trigger an action that
shows the difference in days between the chosen date and the current
date. This will also make use of an NSDate object to store the result returned by the date picker,
its instance method timeIntervalSinceDate to
perform the calculation, and an NSDateFormatter object to format a date so that we can display it in a
user-friendly manner via a single UILabel. Figure 1 shows the
finished application.
By
the Way
Keep in mind that, despite
its name, the NSDate class also
stores the time. The application we create will take into account the
time as well as the date when performing its calculation.
Setting Up the Project
Start
Xcode and create a new application based on the iPhone View-Based
Application template. Name the project DateCalc.
Next, open the
DateCalcViewController.h file and add an outlet and property declaration
for the label (UILabel) that will
display the difference between dates—differenceResult. Next, add an action called showDate. We’ll be calling this when the user changes the
value on the date picker.
The (very simple) interface
file is shown in Listing 1:
Listing 1.
#import <UIKit/UIKit.h>
@interface DateCalcViewController : UIViewController { IBOutlet UILabel *differenceResult; }
@property (nonatomic, retain) UILabel *differenceResult;
-(IBAction)showDate:(id)sender;
@end
|
Switch to the implementation file
(DateCalcViewController.m) and add a corresponding @synthesize
directive for differenceResult, located after the @implementation
line:
@synthesize differenceResult;
By
the Way
Notice that we don’t have
an outlet or property for the date picker itself? As with the segmented
control in the last hour, we’ll just use the sender variable to reference the date picker within the showDate action method. Because nothing else is calling
the method, we know with certainty that sender will always be
the picker.
Because we’ve got a pretty
good handle on the project setup and you’re probably sick of hearing me
go on about it at the end of each project, let’s take care of something
we’ve done last, first: Make sure we’re properly releasing anything
we’ve retained.
For this project, that’s one
object: differenceResult. Edit DateCalcViewController’s dealloc method to read as follows:
- (void)dealloc {
[differenceResult release];
[super dealloc];
}
By
the Way
Prior to this hour, we had
typically handled the final releases at the end of the project to make
sure that it was a step you thought through before calling a project
“done.” Typically, I like to make sure that as soon as I’ve identified
something that needs to be released, the release statement is written
and added to the code. Obviously, you can work through whatever process
is best for your coding style. Just make sure that you follow through
and do it!
Let’s keep up the pace and
move on to the UI and our date picker. After you’ve created the outlet
and the action, save the file, and open DateCalcViewController.xib in
Interface Builder.