iOS 自定义只有年月的DatePikerView
阅读原文时间:2023年07月12日阅读:2

头文件:

@interface YearMonthPikerView : UIView

@property (nonatomic,copy) void(^cancelBlock)();

@property (nonatomic,copy) void(^sureBlock)(NSString*,NSString*);

@end

实现文件:

#import "YearMonthPikerView.h"

static const int loop = ;

@interface YearMonthPikerView()

@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 = @[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""];

    [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-)];

    [self.formatter setDateFormat:@"yyyy"];
    self.currentYear = [self.formatter stringFromDate:currentDate];
    self.selectYear = self.currentYear;

    NSMutableArray *yearsArr = [NSMutableArray array];
    [yearsArr addObject:self.currentYear];

    int nowYear = [self.currentYear intValue];
    for (int i=; i<loop; i++) {
    nowYear = nowYear - ;
    NSString *nowYearStr = [NSString stringWithFormat:@"%d",nowYear];
    [yearsArr insertObject:nowYearStr atIndex:];
    }
    self.yearsArr = yearsArr;
    self.isCurrentYear = YES;
    [self.ymPikerView selectRow:(self.yearsArr.count-) inComponent: animated:YES];
    [self.ymPikerView selectRow:(self.currentMonth.intValue-) inComponent: animated:YES];

}

  • (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2.0;
    }

  • (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component==) {
    return self.yearsArr.count;
    }else if (component==){
    if (self.isCurrentYear) {
    return self.currentMonth.integerValue;
    }else{
    return self.monthsArr.count;
    }
    }else{
    return ;
    }
    }

  • (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component==) {
    NSString *year = self.yearsArr[row];
    year = [year stringByAppendingString:@"年"];
    return year;
    }else if (component==){
    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== && row==self.yearsArr.count-) {
    self.isCurrentYear = YES;
    [self.ymPikerView reloadComponent:];

    self.selectYear = self.yearsArr\[row\];  

    }else if (component==){
    self.isCurrentYear = NO;
    [self.ymPikerView reloadComponent:];

    self.selectYear = self.yearsArr\[row\];  

    }else if (component==){
    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

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章