UIDatePicker的使用

来源:互联网 发布:网络维护基础知识 编辑:程序博客网 时间:2024/05/18 09:02

@property (nonatomic,strong)UIView *background;

@property (nonatomic,strong)UIView *pickerBgView;

@property (nonatomic,strong)UIDatePicker *datePicker;



    _background = [[UIViewalloc]init];

    _background.frame =CGRectMake(0,0,WEIDTH,HEIGHT);

    _background.backgroundColor = [UIColorblackColor];

    _background.alpha =0.5;

    _background.hidden=NO;

    [self.navigationController.viewaddSubview:_background];

    

    

    _pickerBgView = [[UIViewalloc]init];

    _pickerBgView.frame =CGRectMake(0,HEIGHT - 250,WEIDTH,200);

    _pickerBgView.backgroundColor = [UIColorwhiteColor];

    _pickerBgView.hidden=NO;

    [self.navigationController.viewaddSubview:_pickerBgView];

    

    UIButton *cancelBtn = [[UIButtonalloc]init];

    cancelBtn.frame =CGRectMake(10,10,60,20);

    [cancelBtn setTitle:@"取消"forState:UIControlStateNormal];

    [cancelBtn setTitleColor:[UIColorgrayColor]forState:UIControlStateNormal];

    [cancelBtn addTarget:selfaction:@selector(cancelBtnClick)forControlEvents:UIControlEventTouchUpInside];

    [_pickerBgViewaddSubview:cancelBtn];

    

    UIButton *confirmBtn = [[UIButtonalloc]init];

    confirmBtn.frame =CGRectMake(WEIDTH -70,10,60,20);

    [confirmBtn setTitle:@"确定"forState:UIControlStateNormal];

    [confirmBtn setTitleColor:[UIColororangeColor]forState:UIControlStateNormal];

    confirmBtn.tag =1;

    [confirmBtn addTarget:selfaction:@selector(confirmBtnClick)forControlEvents:UIControlEventTouchUpInside];

    [_pickerBgViewaddSubview:confirmBtn];

    

    _datePicker=[[UIDatePickeralloc]initWithFrame:CGRectMake(0,50,WEIDTH, 150)];

    [_datePickersetTimeZone:[NSTimeZonesystemTimeZone]];

    [_datePickeraddTarget:selfaction:@selector(datePickerChangeValue:)forControlEvents:UIControlEventValueChanged];


    [_datePickersetDatePickerMode:UIDatePickerModeDateAndTime];

    [_pickerBgViewaddSubview:_datePicker];


-(void)datePickerChangeValue:(UIDatePicker*)pickerView{

    NSDate *date = pickerView.date;

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    _currentDay = [dateFormatterstringFromDate:date];

    NSLog(@"--%@",_currentDay);

}


-(void)cancelBtnClick{

    _background.hidden=YES;

    _pickerBgView.hidden=YES;

}

-(void)confirmBtnClick{

    _background.hidden=YES;

    _pickerBgView.hidden=YES;

    NSLog(@"--%@",_datePicker.date);

}



原创粉丝点击