zoukankan      html  css  js  c++  java
  • iOS 自定义只有年月的DatePikerView

    头文件:

    @interface YearMonthPikerView : UIView
    
    @property (nonatomic,copy) void(^cancelBlock)();
    
    @property (nonatomic,copy) void(^sureBlock)(NSString*,NSString*);
    
    @end

    实现文件:

    #import "YearMonthPikerView.h"
    
    static const int loop = 20;
    
    @interface YearMonthPikerView()<UIPickerViewDelegate,UIPickerViewDataSource>
    
    @property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
    
    @property (weak, nonatomic) IBOutlet UIButton *sureBtn;
    
    @property (weak, nonatomic) IBOutlet UIPickerView *ymPikerView;
    
    @property (nonatomic,strong) NSArray *monthsArr;   //月份的数组
    
    @property (nonatomic,strong) NSArray *yearsArr;    //年份的数组
    
    @property (nonatomic,strong) NSDateFormatter *formatter;
    
    @property (nonatomic,strong) NSString *currentYear;
    
    @property (nonatomic,strong) NSString *currentMonth;
    
    @property (nonatomic,assign) BOOL isCurrentYear;
    
    @property (nonatomic,strong) NSString *selectYear;
    
    @property (nonatomic,strong) NSString *selectMonth;
    
    @end
    
    @implementation YearMonthPikerView
    
    - (void)awakeFromNib{
        [super awakeFromNib];
        
        self.ymPikerView.delegate = self;
        self.ymPikerView.dataSource = self;
        
        //初始化
        self.monthsArr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
        
        [self.formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] ];
        NSDate *currentDate = [NSDate date];
        
        [self.formatter setDateFormat:@"MM"];
        self.currentMonth = [self.formatter stringFromDate:currentDate];
        self.selectMonth = self.monthsArr[(self.currentMonth.integerValue-1)];
        
        [self.formatter setDateFormat:@"yyyy"];
        self.currentYear = [self.formatter stringFromDate:currentDate];
        self.selectYear = self.currentYear;
        
        NSMutableArray <NSString*>*yearsArr = [NSMutableArray array];
        [yearsArr addObject:self.currentYear];
        
        int nowYear = [self.currentYear intValue];
        for (int i=0; i<loop; i++) {
            nowYear = nowYear - 1;
            NSString *nowYearStr = [NSString stringWithFormat:@"%d",nowYear];
            [yearsArr insertObject:nowYearStr atIndex:0];
        }
        self.yearsArr = yearsArr;
        self.isCurrentYear = YES;
        [self.ymPikerView selectRow:(self.yearsArr.count-1) inComponent:0 animated:YES];
        [self.ymPikerView selectRow:(self.currentMonth.intValue-1) inComponent:1 animated:YES];
        
    }
    
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 2.0;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        if (component==0) {
            return self.yearsArr.count;
        }else if (component==1){
            if (self.isCurrentYear) {
                return self.currentMonth.integerValue;
            }else{
                return self.monthsArr.count;
            }
        }else{
            return 0;
        }
    }
    
    
    - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        if (component==0) {
            NSString *year = self.yearsArr[row];
            year = [year stringByAppendingString:@""];
            return year;
        }else if (component==1){
            NSString *month = self.monthsArr[row];
            month = [month stringByAppendingString:@""];
            return month;
        }else{
            return nil;
        }
    }
    
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        if (component==0 && row==self.yearsArr.count-1) {
            self.isCurrentYear = YES;
            [self.ymPikerView reloadComponent:1];
            
            self.selectYear = self.yearsArr[row];
        }else if (component==0){
            self.isCurrentYear = NO;
            [self.ymPikerView reloadComponent:1];
            
            self.selectYear = self.yearsArr[row];
        }else if (component==1){
            self.selectMonth = self.monthsArr[row];
        }
    }
    
    
    - (IBAction)cancelBtnClick:(UIButton *)sender {
        if (self.cancelBlock) {
            self.cancelBlock();
        }
    }
    
    
    - (IBAction)sureBtnClick:(UIButton *)sender {
        if (self.sureBlock) {
            self.sureBlock(self.selectYear, self.selectMonth);
        }
    }
    
    
    #pragma mark - 懒加载
    - (NSDateFormatter*)formatter{
        if (_formatter==nil) {
            _formatter = [[NSDateFormatter alloc]init];
        }
        return _formatter;
    }
    
    @end

    效果如下图:

    Demo地址如下:https://github.com/LuPing-Kuang/iOS-YearMonthPickerView

  • 相关阅读:
    07.消除过期对象的引用
    1.1进程和多线程概述
    1.2什么是操作系统
    06.避免创建不必要的对象
    05.依赖注入优先于硬连接资源
    04.使用私有构造器执行非实例化
    03.使用私有构造方法或枚类实现 Singleton 属性
    02.当构造参数过多时使用builder模式
    01.考虑使用静态工厂方法替代构造方法
    iiS申请地址
  • 原文地址:https://www.cnblogs.com/Caersi/p/6741987.html
Copyright © 2011-2022 走看看