当前位置: 首页 > 工具软件 > UI Datepicker > 使用案例 >

日期选择UIDatePicker相关

慕才
2023-12-01

//初始日期选择控件
    dataPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 200.0, 0.0, 0.0)];
    [dataPicker setHidden: YES];
    //日期模式
    [dataPicker setDatePickerMode:UIDatePickerModeDate];
    //定义最小日期
    NSDateFormatter *formatter_minDate = [[NSDateFormatter alloc] init];
    [formatter_minDate setDateFormat:@"yyyy-MM-dd"];
    NSDate *minDate = [formatter_minDate dateFromString:@"2004-01-01"];
    formatter_minDate = nil;
    [formatter_minDate release];
    //最大日期是今天
    NSDate *maxDate = [NSDate date];
    
    [dataPicker setMinimumDate:minDate];
    [dataPicker setMaximumDate:maxDate];
    [dataPicker addTarget:self action:@selector(dataValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:dataPicker];
    
    //设定宝宝生日标签默认显示今天
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    lab_Baby_birthday.text = [formatter stringFromDate:maxDate];
    formatter = nil;
    [formatter release];

//datapicker值攺变事件
- (void) dataValueChanged:(UIDatePicker *)sender
{
    UIDatePicker *dataPicker_one = (UIDatePicker *)sender;
    NSDate *date_one = dataPicker_one.date;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    lab_Baby_birthday.text = [formatter stringFromDate:date_one];
    formatter = nil;
    [formatter release];
}


 类似资料: