Labels

Learn the powerful enterprise adaptable database:

Getting Started With ADABAS & Natural

Saturday, March 9, 2013

Xcode 4.6: Date Picker



How to use datePicker in iPhone (UIDatePicker)


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

No comments:

Post a Comment