UI基础 - UIDatePicker
■ 简言
1. UIDatePicker 是时间选择器:它可以获取选择的日期时间、设置显示语言、12/24 小时进制、获取本地时间、限制选择时间的选择范围、格式化显示输出所需要的显示时间等
■ 使用方式
1. 基本使用
1 UIDatePicker *datePicker = [[UIDatePicker alloc] init]; 2 [datePicker setTimeZone:[NSTimeZone defaultTimeZone]]; // 默认时区:格林尼治时间 3 [datePicker setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+8"]];// 时区:中国 4 NSLog(@"%@",datePicker.date); 5 6 datePicker.backgroundColor = [UIColor yellowColor]; 7 datePicker.frame = CGRectMake(20, 20, self.view.frame.size.width - 40, 90);// 宽高系统默认:(0, 0, 320, 216) 8 datePicker.datePickerMode = UIDatePickerModeDate;// 显示样式:月 日 年 9 10 NSLocale *local = [[NSLocale alloc] initWithLocaleIdentifier:@"English"];// 本地语言:英文 11 [datePicker setLocale:local]; 12 13 datePicker.maximumDate = datePicker.date;// 可滚动至最大时间 14 [self.view addSubview:datePicker];
运行效果
2. 有两个文本框,均默认显示当前时间;当点击任一文本框时则弹出时间选择器,将选定的时间显示在文本框中
1 #import "ViewController.h" 2 @implementation ViewController 3 4 - (void)viewDidLoad { 5 [super viewDidLoad]; 6 7 // 时间选择器 8 UIDatePicker *datePicker = [[UIDatePicker alloc] init]; 9 datePicker.backgroundColor = [UIColor orangeColor]; 10 [datePicker setDatePickerMode:UIDatePickerModeDate]; 11 [datePicker addTarget:self action:@selector(showDate:) forControlEvents:UIControlEventValueChanged];// 滑动时间触发 12 // 文本框:显示年、月、日、星期 13 UITextField *dateTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,50, self.view.frame.size.width - 40, 30)]; 14 dateTextField.backgroundColor = [UIColor brownColor]; 15 dateTextField.clearsOnBeginEditing = YES; 16 dateTextField.textAlignment = NSTextAlignmentCenter; 17 dateTextField.borderStyle = UITextBorderStyleRoundedRect; 18 dateTextField.tag = 200; 19 dateTextField.placeholder = @"请选取日期"; 20 // date、String 转换 21 NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init]; 22 formatter1.dateFormat = @"yyyy-MM-dd EE";// 日期格式 23 dateTextField.text = [formatter1 stringFromDate:datePicker.date]; 24 dateTextField.inputView = datePicker; 25 [self.view addSubview:dateTextField]; 26 27 28 // 时间选择器 29 UIDatePicker *timePicker = [[UIDatePicker alloc] init]; 30 timePicker.backgroundColor = [UIColor yellowColor]; 31 [timePicker setDatePickerMode:UIDatePickerModeTime]; 32 NSLocale *local = [[NSLocale alloc] initWithLocaleIdentifier:@"Chinese"]; 33 [timePicker setLocale:local]; 34 [timePicker addTarget:self action:@selector(showTime:) forControlEvents:UIControlEventValueChanged]; 35 // 文本框:显示时、分、秒 36 UITextField *timeTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 120, self.view.frame.size.width - 40, 30)]; 37 timeTextField.placeholder = @"请选择时间"; 38 timeTextField.clearsOnBeginEditing = YES; 39 timeTextField.textAlignment = NSTextAlignmentCenter; 40 timeTextField.borderStyle = UITextBorderStyleRoundedRect; 41 timeTextField.tag = 201; 42 timeTextField.inputView = timePicker; 43 // date、String 转换 44 NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init]; 45 formatter2.dateFormat = @"hh:mm:ss-aa"; 46 timeTextField.text = [formatter2 stringFromDate:timePicker.date]; 47 timeTextField.inputView = timePicker; 48 [self.view addSubview:timeTextField]; 49 50 } 51 52 53 // 显示日期 54 - (void)showDate:(UIDatePicker *)datePicker{ 55 56 UITextField *dateTextField = (UITextField *)[self.view viewWithTag:200]; 57 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 58 formatter.dateFormat = @"yyyy-MM-dd EE"; 59 dateTextField.text = [formatter stringFromDate:datePicker.date]; 60 } 61 62 // 显示具体时间 63 - (void)showTime:(UIDatePicker *)timePicker{ 64 65 UITextField *timeTextField = (UITextField *)[self.view viewWithTag:201]; 66 NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; 67 formatter.dateFormat = @"hh:mm:ss-aa"; 68 timeTextField.text = [formatter stringFromDate:timePicker.date]; 69 } 70 71 72 // 键盘回收 73 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 74 75 [self.view endEditing:YES]; 76 } 77 78 79 @end
运行效果:年月日 | 时分
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律