转自:http://www.cocoachina.com/bbs/simple/?t70445.html
初始化代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. NSDate *now = [NSDate date]; [_datePicker setDate:now animated:NO]; }
显示代码:
- (IBAction)buttonPressed { NSDate *selected = [_datePicker date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"]; NSString *theDate = [dateFormat stringFromDate:selected]; NSString *message = [[NSString alloc] initWithFormat:@"The date and time you selected is: %@", theDate]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Date and Time Selected" message:message delegate:nil cancelButtonTitle:@"Yes, I did." otherButtonTitles:nil]; [alert show]; }