In this tutorial will learn how to use iPhone Date Picker to show current date and is displayed using alert view. To show this we have to take button and on that will write the action and in that will write the method to show the date which you select on the iPhone date picker with time. The Date Picker in iPhone can be selected from the Library and add it to the view and to check the attributes for the Date Picker select the Date Picker and then click tools -> attributes inspector.
Final View will look like this:
My project name is DatePickerViewController and is based on view application. Here we are using Date Picker to show the date or to show the selected date by the user with the click on the button, so will create variable for the Date Picker and will write the action for the button to show the selected Date.
Add this to view controller.h file:
IBOutlet UIDatePicker *picker;
After writing the action in .h file will write the method for the action in .m file, in action will use alert view to show the date or selected date by the user and for that will create variable and after using will release the variable. Then in view did load method will set the date into the picker.
Add this to view controller.m file:
NSDate *selected = [picker date];
NSString *message = [[NSString alloc] initWithFormat:@"DATE AND TIME IS: %@", selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SELECTED DATE AND TIME" message:message delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
// add in view did load method.
NSDate *now = [NSDate date];
[picker setDate:now animated:YES];
After doing all this will make the connection in Interface Builder and run the program or project.
Make connection with Interface Builder:
Finally press Build And Go button.
Posted on: September 25, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles