效果
初始化
UIDatePicker *pick = [[UIDatePicker alloc] init];
属性
1. 模式
pick.datePickerMode = UIDatePickerModeTime;
可写属性有
UIDatePickerModeTime, // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM) UIDatePickerModeDate, // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007) UIDatePickerModeDateAndTime, // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM) UIDatePickerModeCountDownTimer, // Displays hour and minute (e.g. 1 | 53)
监听
1. 添加监听
[pick addTarget:self action:@selector(setTimeOK:) forControlEvents:UIControlEventValueChanged];
2. 实现监听
- (void)setTimeOK:(UIDatePicker *)sender{ NSDate *date = sender.date; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSString *dateStr = [formatter stringFromDate:date]; self.textField.text = [NSString stringWithFormat:@"%@", dateStr]; }
typedef enum { NSDateFormatterNoStyle = kCFDateFormatterNoStyle, NSDateFormatterShortStyle = kCFDateFormatterShortStyle,//“11/23/37” or “3:30pm” NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle,//\"Nov 23, 1937\" NSDateFormatterLongStyle = kCFDateFormatterLongStyle,//\"November 23, 1937” or “3:30:32pm\" NSDateFormatterFullStyle = kCFDateFormatterFullStyle//“Tuesday, April 12, 1952 AD” or “3:30:42pm PST” } NSDateFormatterStyle;
IOS控件 UIDatePicker
时间: 2024-11-03 20:16:29