[iOS datePickerView] UIDatePickerView 的简单用法

  //设置datePickerView模式
    _datePickerView.contentMode = UIDatePickerModeDate;
    //设置显示默认当天
    _datePickerView.calendar = [NSCalendar currentCalendar];
       //设置 显示当前的时间 
    _timeTitleLab.text = [self formatterDate:[NSDate date]];
    //设置监听事件
    [_datePickerView addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
//NSDate 转换 为 时间字符串
-(NSString*)formatterDate:(NSDate*)date{
    NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init];
    [dataFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateTime = [dataFormatter stringFromDate:date];
    return dateTime;
}


//触及页面灰色部分,页面消失
- (IBAction)hiddenBtn:(id)sender {
}


//回到今天 按钮
- (IBAction)goBackToday:(id)sender {

   [ _datePickerView setDate:todayDate animated:YES];
    _timeTitleLab.text = [self formatterDate:todayDate];
}
//确认按钮
- (IBAction)sureBtn:(id)sender {
    NSLog(@"%@",_timeTitleLab.text);

}

- (void)changeValue:(UIDatePicker*)sender{
       _timeTitleLab.text = [self formatterDate:[sender date]];
}

补充一下
获取当前时间的方法 [NSDate date] 就这么简单““`
[iOS datePickerView] UIDatePickerView 的简单用法_第1张图片

你可能感兴趣的:(#,iOS,系统控件)