RSS

How to use datePicker in iPhone (UIDatePicker)

10 Jul

Step 1: The first thing we need to do is to  indicate that our view controller would be acting as a UITextField. To do this you simply need to declare it in the interface file:

#import <UIKit/UIKit.h>
@interface DatePickerViewController : UIViewController {
IBOutlet UIDatePicker *datePicker;
}
@end

Step 2: Now,double click .xib file and open Interface Builder. We need a date picker,so look for Date Picker in the Library,and drag one over to the View Window .Then Save close the nib and go back to xcode.

Step 3: In the ViewDidLoad,We create new NSDate object. An NSDate object created this way will hold the current date and time. We then set datePicker to that date,which ensure every time this view loads, the Picker will reset to the current date and time.

– (void)viewDidLoad {
NSDate *now = [[NSDate alloc] init];
[datePicker setDate:now animated:YES];
[now release];
[super viewDidLoad];
}

Step 4: Double click .xib file and open Interface Builder.  Grab Round Rect Button from the Library,and place it below the Date Picker.Double click it,and give it a title of “Select”. Save,close the nib, and go to the xcode.

Step 5: We added the implementation of button Pressed and we overrode viewDidLoad.

-(IBAction)buttonPressed:(id)sender{
NSDate *selected =[datePicker date];
NSString *message =[[NSString alloc] initWithFormat:
@”The Date and Time you selected is: %@”,selected];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:
@”Date and Time Selected”
message:message
delegate:nil
cancelButtonTitle:@”Yes, I did.”
otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
}
You can Download SourceCode from here DatePicker
Advertisements
 
Leave a comment

Posted by on July 10, 2011 in iPhone

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: