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
  •  
    Most View
    Windows System Programming : Exception Handling - Treating Errors as Exceptions
    GeForce GTX 660 Graphics Cards Roundup (Part 5)
    Some Of The Biggest Brands In The World Had Their Products (Part 10)
    How to Buy…A TV TUNER (Part 2)
    iPad Keyboard Cases Selection
    Windows Server 2008 R2 networking : Routing and Remote Access
    Programming Windows Services with Microsoft Visual Basic 2008 : Extending the WMI Implementation
    Understanding the Architecture of SharePoint 2010 : Capabilities
    Attractive Apps For Black Berry 10 (Part 1)
    Swann OutbackCam Security Camera
    Top 10
    Ascend D1 Quad XL - Huawei Quad-Core Smartphone (Part 4)
    Ascend D1 Quad XL - Huawei Quad-Core Smartphone (Part 3)
    Ascend D1 Quad XL - Huawei Quad-Core Smartphone (Part 2)
    Ascend D1 Quad XL - Huawei Quad-Core Smartphone (Part 1)
    LG Optimus G - A Quad-Core Flagship With Nexus Aspiration (Part 4)
    LG Optimus G - A Quad-Core Flagship With Nexus Aspiration (Part 3)
    LG Optimus G - A Quad-Core Flagship With Nexus Aspiration (Part 2)
    LG Optimus G - A Quad-Core Flagship With Nexus Aspiration (Part 1)
    The Assemblage Of GeForce GTX 650 Ti Graphics Cards (Part 7)
    The Assemblage Of GeForce GTX 650 Ti Graphics Cards (Part 6)