建立Outlet datePicker。
以同样方式给Button建立一个Action关联映射,命名为selectDate,事件类型为默认的Touch Up Inside。
4、实现代码
单击ViewController.m,找到刚才创建的
- (IBAction)selectDate:(id)sender {
}
在这里添加响应代码
复制代码
- (IBAction)selectDate:(id)sender {
NSDate *select = [datePicker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *dateAndTime = [dateFormatter stringFromDate:select];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"时间提示" message:dateAndTime delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
运行看效果:
5、修改模式成Date模式,修改代码
复制代码
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
常用参数
上面已经提到了一些常用参数的使用,下面再来列一下比较常用的几个:
1.Locale
设置DatePicker的地区,即设置DatePicker显示的语言。
跟踪所有可用的地区,取出想要的地区
复制代码NSLog(@"%@", [NSLocale availableLocaleIdentifiers]);
2. 设置日期选择控件的地区
复制代码
[datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];












