用 UIPickerView 自定义年,月选取框

1.首先要遵守

UIPickerViewDataSource, UIPickerViewDelegate 这两个协议

2.所需要的对象

@property (nonatomic ,strong) NSMutableArray *year_Array;

@property (nonatomic ,strong) NSArray *according_moth_Array;

@property (nonatomic ,strong) NSArray *actual_starting_moth_Array;

//@property (nonatomic ,strong) NSArray *actual_termination_moth_Array;

@property (nonatomic ,strong) NSString *current_year_string;

@property (nonatomic ,strong) NSString *current_moth_string;

@property (nonatomic, strong) UIPickerView *datePicker;

 3.设置时间和记录当前时间

- (void)createDate
{
    NSDate *current_time = [NSDate date];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy"];
    
    self.current_year_string = [NSString stringWithFormat:@"%@年",[formatter stringFromDate:current_time]];
    
    [formatter setDateFormat:@"MM"];
    
    self.current_moth_string = [NSString stringWithFormat:@"%@月",[formatter stringFromDate:current_time]];
    
    for (int32_t i = 1970; i<= 2050; i++) {
        
        [self.year_Array addObject:[NSString stringWithFormat:@"%d年",i]];
    }
    
    self.according_moth_Array = @[@"01月",@"02月",@"03月",@"04月",@"05月",@"06月",@"07月",@"08月",@"09月",@"10月",@"11月",@"12月"];
    self.actual_starting_moth_Array =@[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"];
//    self.actual_termination_moth_Array = @[@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"01"];
    
}

 4.初始化 UIPickerView

self.datePicker = [[UIPickerView alloc] init];
    
    self.datePicker.frame = CGRectMake(0, 0, ScreenWidth - 80, 162);
    
    self.datePicker.delegate = self;
    
    self.datePicker.dataSource = self;
    
    [self.datePicker selectRow:[self.year_Array indexOfObject:self.current_year_string] inComponent:0 animated:YES];
    
    [self.datePicker selectRow:[self.according_moth_Array indexOfObject:self.current_moth_string] inComponent:1 animated:YES];

 5.实现

UIPickerViewDatasource

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *pickerLabel = (UILabel *)view;
    
    if (pickerLabel == nil) {
        
        CGRect frame = CGRectMake(0.0, 0.0, 100, 60);
        
        pickerLabel = [[UILabel alloc] initWithFrame:frame];
        
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        
        [pickerLabel setFont:[UIFont systemFontOfSize:22.0f]];
    }
    
    if (component == 0)
    {
        pickerLabel.text =  [self.year_Array objectAtIndex:row];
    }
    else
    {
        pickerLabel.text =  [self.according_moth_Array objectAtIndex:row];
    }
    
    return pickerLabel;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return 100;
    } else {
        return 100;
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        return [self.year_Array count];
    }
    else
    {
        return [self.according_moth_Array count];
    }
}

 6. 获取显示的时间

self.beginTimeTF.text = [NSString stringWithFormat:@"%d-%@",[[self.year_Array objectAtIndex:[self.datePicker selectedRowInComponent:0]] intValue],[self.actual_starting_moth_Array objectAtIndex:[self.datePicker selectedRowInComponent:1]]];

 

转载于:https://www.cnblogs.com/xiaoxiaolei/p/4964326.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值