Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

UIDatePicker的使用

$
0
0

UIDatePicker的使用

- (HLSettingLabeltemModel *)startModel{
    if (nil == _startModel) {
        HLSettingLabeltemModel *labelModel = [HLSettingLabeltemModel itemModelWithTitle:@"开始时间" icon:@""];
        [labelModel setText:@"00:00"];
        /**
         定义展示键盘的block
         */
        [labelModel setOptionBlock:^{
            UITextField *tmpTextField = [[UITextField alloc]init];
            [self.view addSubview:tmpTextField];
            //设置键盘类型
            UIDatePicker *datePicker = [[UIDatePicker alloc]init];
            [datePicker setDatePickerMode:UIDatePickerModeTime];
            NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
            [datePicker setLocale:locale];//本地化
            //设置日期格式
            NSDateFormatter *dateF = [[NSDateFormatter alloc]init];
            [dateF setDateFormat:@"HH:mm"];
            NSDate *date = [dateF  dateFromString:@"01:00"];//设置默认日期
            [datePicker setDate:date animated:YES];
            [tmpTextField setInputView:datePicker];
            //监听事件滚动
            [datePicker addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
            //展示日期键盘
            [tmpTextField becomeFirstResponder];
        }];
        _startModel = labelModel;
    }
    return _startModel;
}

pragma mark - valueChange 处理日期选择

- (void) valueChange:(UIDatePicker*)datePicker{
    //修改模型
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH:mm"];
    [self.startModel setText:[formatter stringFromDate:datePicker.date]];
    //刷新数据
    [self.tableView reloadData];
    NSLog(@"%@",datePicker.date);
}
作者:u011018979 发表于2017/7/3 15:21:13 原文链接
阅读:26 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>