MOBILE

iPhone Application Development : Making Multivalue Choices with Pickers - Using Date Pickers (part 1)

2/25/2011 9:49:56 PM
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.

Figure 1. The sample application will use a single date picker and a label as its UI.


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.

Other  
  •  iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
  •  Sync Your iPad with iTunes : Troubleshooting iTunes and the Sync
  •  Sync Your iPad with iTunes : Manually Transferring Music, Movies, Podcasts, and More on Your iPad (Drag-and-Drop Method)
  •  Windows Phone 7 Development : Internationalization - Using Resource Files to Localize Content
  •  Windows Phone 7 Development : Internationalization - Storing and Retrieving Current Culture Settings
  •  Mobile Application Security : WebOS Security - Development and Security Testing
  •  Mobile Application Security : WebOS Security - Introduction to the Platform
  •  iPhone Application Development : Getting the User’s Attention - Using Alert Sounds and Vibrations
  •  iPhone Application Development : Getting the User’s Attention - Using Action Sheets
  •  jQuery 1.3 : Modifying table appearance (part 4) - Filtering
  •  jQuery 1.3 : Modifying table appearance (part 3) - Collapsing and expanding sections
  •  jQuery 1.3 : Modifying table appearance (part 2) - Tooltips
  •  jQuery 1.3 : Modifying table appearance (part 1) - Row highlighting
  •  Windows Phone 7 Development : Using Culture Settings with ToString to Display Dates, Times, and Text
  •  Mobile Application Security : SymbianOS Security - Persistent Data Storage
  •  Mobile Application Security : SymbianOS Security - Interprocess Communication
  •  Mobile Application Security : SymbianOS Security - Permissions and User Controls
  •  Windows Phone 7 Development : Building a Trial Application (part 3) - Verifying Trial and Full Mode & Adding Finishing Touches
  •  Windows Phone 7 Development : Building a Trial Application (part 2) - Connecting to a Web Service & Adding Page-to-Page Navigation
  •  Windows Phone 7 Development : Building a Trial Application (part 1) - Building the User Interface
  •  
    Top 10
    The Most Attractive Popular Cellphones In 2012
    Windows Phone 8 Operating System Review – Part2
    Windows Phone 8 Operating System Review – Part1
    LaCie Rugged USB 3.0 Thunderbolt
    Best Photo Printers Revealed – Jan 2013 (Part 6)
    Best Photo Printers Revealed – Jan 2013 (Part 5) : Epson Stylus Photo R3000
    Best Photo Printers Revealed – Jan 2013 (Part 4) : Epson Stylus Photo R2000, Canon PIXMA Pro-1
    Best Photo Printers Revealed – Jan 2013 (Part 3) : Epson stylus photo PX830FWD, Canon PIXMA Pro9000 Mark II
    Best Photo Printers Revealed – Jan 2013 (Part 2) : Canon PIXMA MG6250
    Best Photo Printers Revealed – Jan 2013 (Part 1)
    Most View
    Programming Excel with VBA and .NET : Variables (part 1) - Names & Declarations
    Infrastructure Security: The Host Level
    Creative software
    Mobile Application Security - BlackBerry Security - Permissions and User Controls (part 2)
    The Android Army invades! (Part 2) : Wipe out WebOS
    SSD Supertest – December 2012 (Part 2) : Intel 330 Series 180GB
    Exploiting SQL Injection : Enumerating the Database Schema (part 3) - Oracle
    The best music apps for your iOS Device (Part 3) - ITUNES U, VOXER
    Group Test: Android Tablets – November 2012 (Part 3) - Toshiba AT300-101
    LG Optimus 3D Max Review (Part 1)
    SQL Server 2008 : OPENXML, sp_xml_preparedocument, and sp_xml_removedocument
    Windows Vista : Performance - Hard Disk (part 1) - A Defragmentation Crash Course
    These Companies Would Still Be Here In 5 Years
    Can You Mix Your Own Cocktail Using Your Android Phone?
    Armaggeddon Nighthawk Kai-5 - The Fleldging Nighthawk
    Tt eSports Level 10M Gaming Mouse - Unlike Any Other
    Windows 7 : Maintaining Your System Configuration (part 3) - Configuring User Profiles, Environment Variables, and Startup and Recovery
    Windows Server 2008: Domain Name System and IPv6 - Troubleshooting DNS
    AOC MyStage E2343Fi 23" LED Monitor
    Mobile Phone Game Programming : Using Sprite Animation - Achieving Smooth Animation with the GameCanvas Class