美文网首页
ios 时间选择器

ios 时间选择器

作者: Satanshun | 来源:发表于2017-12-02 11:43 被阅读0次

    //日期选择器

    @property (nonatomic,strong)UIDatePicker *myDatePicker;

    //记录当前选中时间

    @property (nonatomic,strong)NSString *recordTime;

    //懒加载

    [self.View addSubview:self.myDatePicker];

    //初始化

    - (UIDatePicker *)myDatePicker

    {

    if (!_myDatePicker) {

    _myDatePicker = [[UIDatePicker alloc]initWithFrame:DrawingAdapter(0, 51, 375, 367)];

    NSTimeInterval secondPerDay = 24 * 60 * 60;

    NSTimeInterval secondPer2Year = secondPerDay * 356;

    NSDate *today = [[NSDate alloc]init];

    NSDate *min = [today dateByAddingTimeInterval:0];//最小时间

    NSDate *max = [today dateByAddingTimeInterval:secondPer2Year];//最大时间

    _myDatePicker.minuteInterval = 10;//每10分一跳

    [_myDatePicker setLocale:[[NSLocale alloc] initWithLocaleIdentifier: @"zh_CN"]];

    [_myDatePicker setDatePickerMode:UIDatePickerModeDate];//选择器模式

    [_myDatePicker setDate:today animated:YES];

    [_myDatePicker setMinimumDate:min];

    [_myDatePicker setMaximumDate:max];

    [_myDatePicker addTarget:self action:@selector(onDatePickerChanged:) forControlEvents:UIControlEventValueChanged];

    }

    return _myDatePicker;

    }

    //选择器调用

    - (void)onDatePickerChanged:(UIDatePicker *)datePicker {

    NSDate *select = datePicker.date;

    NSDateFormatter *selectDateFormatter = [[NSDateFormatter alloc]init];

    selectDateFormatter.dateFormat = @"yyyy-MM-dd HH:hh:ss";

    NSString *dateAndTime = [selectDateFormatter stringFromDate:select];

    _recordTime = dateAndTime;

    }

    //模式分类

    1、UIDatePickerModeTime 

    2、UIDatePickerModeDate

    3、UIDatePickerModeDateAndTime

    相关文章

      网友评论

          本文标题:ios 时间选择器

          本文链接:https://www.haomeiwen.com/subject/udhnbxtx.html