@interfaceViewController ()
{
UIDatePicker *_datePicker;
UITextField *_textFiled;
NSString *_strTime;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
_textFiled = [[UITextFieldalloc] initWithFrame:CGRectMake(60,80, 150,40)];
_textFiled.backgroundColor = [UIColorcolorWithRed:0.310green:0.913blue:1.000alpha:1.000];
_textFiled.placeholder =@"时间";
[self.viewaddSubview:_textFiled];
_textFiled.borderStyle =UITextBorderStyleRoundedRect;
// Do any additional setup after loading the view.
_datePicker = [[UIDatePickeralloc] init];
// 设置中心
[_datePickersetCenter:self.view.center];
// 设置类型
[_datePickersetDatePickerMode:UIDatePickerModeDateAndTime];
// 设置背景样色
_datePicker.backgroundColor = [UIColorcolorWithRed:0.504green:1.000blue:0.970alpha:1.000];
// 点击事件
[_datePickeraddTarget:selfaction:@selector(pickerAction:)forControlEvents:UIControlEventValueChanged];
[self.viewaddSubview:_datePicker];
}
- (void)pickerAction:(UIDatePicker *)picker
{
NSDateFormatter *formatter = [[NSDateFormatteralloc] init];
formatter.dateFormat =@"yyyy-MM-dd HH:mm";
_strTime = [formatter stringFromDate:_datePicker.date];
_textFiled.text =_strTime;
}
效果图: