#import "RootViewController.h" @interface RootViewController () @property (nonatomic, retain) UITextField *textField; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; //使用 DatePicker自定义键盘 [self customTextFieldForDatePicker]; } #pragma mark - UIDatePicker 日期选择器 //通常用于自定义键盘 - (void)customTextFieldForDatePicker { /** 创建一个 UITextField*/ self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 40)]; self.textField.tag = 100; self.textField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:self.textField]; [self.textField release]; /** 创建一个 Button*/ UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.frame = CGRectMake(120, 150, 100, 30); [btn setTitle:@"按钮" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview: btn]; #pragma mark - ①初始化方法(高是固定的) UIDatePicker *picker = [[UIDatePicker alloc] init]; picker.tag = 200; #pragma mark - ②设置显示模型 /*UIDatePickerModeTime 只显示时间 UIDatePickerModeDate 只显示日期 UIDatePickerModeDateAndTime 日期和时间 UIDatePickerModeCountDownTimer 小时和分钟*/ picker.datePickerMode = UIDatePickerModeDate; #pragma mark - ③设置区域语言(设置12/24小时制是根据手机本身时间显示) picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]; #pragma mark - ④设置默认日历( 默认为当天) picker.calendar = [NSCalendar currentCalendar]; //picker.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese]; #pragma mark - ⑤设置显示的日期 picker.date = [NSDate date]; [picker setDate:[NSDate date] animated:YES]; #pragma mark - ⑥设置时区(默认为0时区) //picker.timeZone = [NSTimeZone defaultTimeZone]; picker.timeZone = [NSTimeZone timeZoneWithName:@"GMT+8"]; NSLog(@"%@", picker.date); #pragma mark - ⑦最小时间 NSDateFormatter *fMinDate = [[NSDateFormatter alloc] init]; [fMinDate setDateFormat:@"yyyy-MM-dd HH:mm"]; NSDate *minDate = [fMinDate dateFromString:@"2015-09-09 08:20"]; [fMinDate release]; picker.minimumDate = minDate; #pragma mark - ⑧最大时间 picker.maximumDate = [NSDate date]; #pragma mark - ⑨倒计时时长(模型设置为UIDatePickerModeCountDownTimer) picker.countDownDuration = 2; #pragma mark - ⑩设置分钟间隔(默认为1,设置的间隔要能被60整除) picker.minuteInterval = 1; #pragma mark - addTarget:action:.. [picker addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged]; //自定义键盘 self.textField.inputView = picker; [picker release]; } #pragma mark - UIDatePicker的 Target方法 - (void)changeValue:(UIDatePicker *)picker { //获取当前UIPickerDate所在的时间 NSDate *date = [picker date]; UITextField *textField = (UITextField *)[self.view viewWithTag:100]; /*//创建日期转换工具 NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; //设置时区 formatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT+8"]; //设定日期转换格式 formatter.dateFormat = @"yyyy年MM年dd日 hh:mm:ss"; textField.text = [formatter stringFromDate:date];*/ textField.text = [NSString stringWithFormat:@"%@", date]; } #pragma mark - 方法 - (void)handleAction:(UIButton *)sender { //获得 UIDatePicker UIDatePicker *picker = (UIDatePicker *)self.textField.inputView; //设置日期 - (void)setDate:(NSDate *)date animated:(BOOL)animated; //[picker setDate:[NSDate date] animated:NO]; picker.date = [NSDate date]; //刷新数据 [self changeValue:picker]; } //编辑结束,回收键盘 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } /*NSCalendar设置日历 + (id)currentCalendar; 取得当前用户的逻辑日历(logical calendar) + (id)autoupdatingCurrentCalendar; 取得当前用户的逻辑日历(logical calendar), ...... - (id)initWithCalendarIdentifier:(NSString *)identifier; 初始化为各种日历。identifier的范围可以是: NSCalendarIdentifierGregorian 阳历 NSCalendarIdentifierBuddhist 佛历 NSCalendarIdentifierChinese 中国日历 NSCalendarIdentifierHebrew 希伯来日历 NSCalendarIdentifierIslamic *日历 NSCalendarIdentifierIslamicCivil *民事日历 NSCalendarIdentifierJapanese 日本日历 */ @end