标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (void)pickerDidChange:(UIDatePicker *)sender; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 [self layoutUI]; 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (void)layoutUI { 22 UIDatePicker *datePChoice = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; 23 datePChoice.center = self.view.center; 24 //设置时间间隔为30分钟,要求必须被60整除的正整数 25 datePChoice.minuteInterval = 30; 26 27 [datePChoice addTarget:self 28 action:@selector(pickerDidChange:) 29 forControlEvents:UIControlEventValueChanged]; 30 [self.view addSubview:datePChoice]; 31 } 32 33 - (void)pickerDidChange:(UIDatePicker *)sender { 34 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 35 [formatter setDateFormat:@"yyyy/MM/dd HH:mm"]; //2015/04/19 13:42 36 NSString *strCurrentDate = [formatter stringFromDate:sender.date]; 37 38 UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"选择的日期" 39 message:strCurrentDate 40 delegate:nil 41 cancelButtonTitle:nil 42 otherButtonTitles:@"确定", nil]; 43 [alertV show]; 44 } 45 46 @end
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4579219.html