iOS-自定义日期并进行卡控

@interfaceViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

{
    BirthView       *birthView;
    UIView          *topView;
    UILabel         *lbl_birth;
    NSMutableArray  *yearArray;
    NSArray         *monthArray;
    NSMutableArray  *DaysArray;
    NSString        *currentMonthString;
    NSString        *currentyearString;
    NSString        *currentDateString;
    NSString        *birthString;
    NSString        *year;
    NSString        *month;
    NSString        *day;
    int             selectedYearRow;
    int             selectedMonthRow;
    int             selectedDayRow;
    BOOL            firstTimeLoad;
    int             currentDay;
    int             currentYear;
    int             currentMonth;
    int             yearRow;
    int             monthRow;
    int             dayRow;
}

@end

@implementation ViewController

#pragma mark - view

- (void)viewDidLoad {
    [superviewDidLoad];
    //创建子视图
    [selfaddSubViews];
}

- (void)addSubViews
{
    //按钮
    UIButton *btn_showBirth = [UIButtonbuttonWithType:UIButtonTypeCustom];
    btn_showBirth.backgroundColor = [UIColoryellowColor];
    [btn_showBirth setTitle:@"选择日期"forState:UIControlStateNormal];
    [btn_showBirth setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
    [btn_showBirth addTarget:selfaction:@selector(showAction)forControlEvents:UIControlEventTouchUpInside];
    [self.viewaddSubview:btn_showBirth];
    [btn_showBirth mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(100);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(@(100));
        make.height.mas_equalTo(@(40));
    }];

    //显示选择的日期
    lbl_birth = [[UILabelalloc]init];
    lbl_birth.backgroundColor = [UIColorredColor];
    lbl_birth.textColor = [UIColorblackColor];
    [self.viewaddSubview:lbl_birth];
    [lbl_birthmas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btn_showBirth.mas_top).offset(100);
        make.centerX.equalTo(btn_showBirth.mas_centerX);
        make.width.mas_equalTo(@(300));
        make.height.mas_equalTo(@(40));
    }];
}

- (void)showAction
{
    [selfshowBirthDate];
}

#pragma mark- 生日选择器
- (void)showBirthDate
{
    //覆盖导航栏,将View添加在keyWindow上
    UIWindow *win = [[UIApplicationsharedApplication] keyWindow];
    topView = [win.subviewsfirstObject];

    birthView = [[BirthViewalloc]init];
    [birthViewcreateViewOnSuperView:topView];
    birthView.datePicker.delegate =self;
    birthView.datePicker.dataSource =self;  
    [birthView.cancelBtnaddTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
    [birthView.finishBtnaddTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
    [self setDate];
}

//
- (void)setDate
{
    //拿到当前日期
    NSDate *date = [NSDatedate];    
    //获取当前年份
    NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
    [formatter setDateFormat:@"yyyy"];
    currentyearString = [NSStringstringWithFormat:@"%@",[formatterstringFromDate:date]];   
    //获取当前月份
    [formatter setDateFormat:@"MM"];
    currentMonthString = [NSStringstringWithFormat:@"%d",[[formatterstringFromDate:date]intValue]];
    
    //获取当前日
    [formatter setDateFormat:@"dd"];
    currentDateString = [NSStringstringWithFormat:@"%@",[formatterstringFromDate:date]];
    currentDay = [currentDateStringintValue];
    yearArray = [[NSMutableArrayalloc]init];

    //当前年份往前推 100年
    currentYear = [currentyearStringintValue];
    for (int i =currentYear-100; i <=currentYear ; i++)
    {
       [yearArrayaddObject:[NSStringstringWithFormat:@"%d年",i]];
    }

    monthArray =@[@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月"];  
    DaysArray = [[NSMutableArrayalloc]init];
    for (int i =1; i <= 31; i++)
    {
        [DaysArrayaddObject:[NSStringstringWithFormat:@"%d日",i]];     
    } 

    //输出row为 2147483647  NSUIntesger
    //int row = (int)[yearArray indexOfObject:currentyearString];  
    //输出row为 2147483647  NSUIntesger
    //int row = (int)[yearArray indexOfObject:currentyearString]; 
    BOOL isHaveYear = [yearArraycontainsObject:[NSStringstringWithFormat:@"%@年",currentyearString]];
    if (isHaveYear) {
        [birthView.datePickerselectRow:[yearArrayindexOfObject:[NSStringstringWithFormat:@"%@年",currentyearString]] -10 inComponent:0animated:YES];//TODO:

    }

    BOOL isHaveMonth = [monthArraycontainsObject:[NSStringstringWithFormat:@"%@月",currentMonthString]];
    if (isHaveMonth) {
        [birthView.datePickerselectRow:[monthArrayindexOfObject:[NSStringstringWithFormat:@"%@月",currentMonthString]]inComponent:1animated:YES];

    }

    
    BOOL isHaveDay =[DaysArraycontainsObject:[NSStringstringWithFormat:@"%@日",currentDateString]];
    if (isHaveDay) {
        [birthView.datePickerselectRow:[DaysArrayindexOfObject:[NSStringstringWithFormat:@"%@日",currentDateString]]inComponent:2animated:YES];

    }

    yearRow = (int)[yearArrayindexOfObject:[NSStringstringWithFormat:@"%@年",currentyearString]];
    monthRow = (int)[monthArrayindexOfObject:[NSStringstringWithFormat:@"%@月",currentMonthString]];
    dayRow = (int)[DaysArrayindexOfObject:[NSStringstringWithFormat:@"%@日",currentDateString]];
    [birthView.datePickerreloadAllComponents];
}


#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component ==0)
    {
        return [yearArraycount];
    }
    elseif (component == 1)
    {
        return [monthArraycount];
    }
    elseif (component == 2)
    {
        //判断 day
        if (firstTimeLoad)
        {
            if (currentMonth ==1 || currentMonth ==3 || currentMonth ==5 || currentMonth ==7 || currentMonth ==8 || currentMonth ==10 || currentMonth ==12)
            {
                //1、3、5、7、8、10、12 都是31天
                return 31;
            }
            elseif (currentMonth ==2)
            {
                //2月 判断平年、闰年
                int yearint = [[yearArrayobjectAtIndex:selectedYearRow]intValue ];
                if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){           
                    return 29;
                }
                else
                {
                    return 28;// or return 29
                }
            }
            else
            {
                return 30;
            }
        }
        else
        {
            if (selectedMonthRow ==0 || selectedMonthRow ==2 || selectedMonthRow ==4 || selectedMonthRow ==6 || selectedMonthRow ==7 || selectedMonthRow ==9 || selectedMonthRow ==11)
            {
               return 31;
            }
            elseif (selectedMonthRow ==1)
            {
                int yearint = [[yearArrayobjectAtIndex:selectedYearRow]intValue ];
                if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){
                    return 29;
                }
                else
                {
                    return 28;
                }
            }
            else
            {
                return 30;
            }            
        }
    }
    return0;
}

- (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, 60,60);
        pickerLabel = [[UILabelalloc] initWithFrame:frame];
       [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColorclearColor]];
        [pickerLabel setFont:[UIFontsystemFontOfSize:15.0f]];
    }
    if (component ==0)
    {
        pickerLabel.text =  [yearArrayobjectAtIndex:(int)row];// Year
    }
    elseif (component == 1)
    {
        pickerLabel.text =  [monthArrayobjectAtIndex:(int)row]; // Month
    }
    elseif (component == 2)
    {
        pickerLabel.text =  [DaysArrayobjectAtIndex:(int)row];// Date
    }
    return pickerLabel;
}

//卡控(当前日期之后日期不能选择)
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    /**
     *  如果年、月、日 >当前日期,那么将默认值置为未修改之前的日期值  或者当前日期最大值
     */
    if (component ==0)
    {
        if (row >=yearRow)
       {
            selectedYearRow =yearRow;
            //选择年 >=当前年,再判断月、日,
            if (selectedMonthRow >monthRow)
            {
               selectedMonthRow = monthRow;
                [birthView.datePickerselectRow:selectedMonthRowinComponent:1animated:YES];
                if (selectedDayRow >dayRow)
                {
                    selectedDayRow = dayRow;
                    [birthView.datePickerselectRow:selectedDayRowinComponent:2animated:YES];
                }
            }
            else
            {
                if (selectedDayRow >dayRow)
                {
                    selectedDayRow = dayRow;
                    [birthView.datePickerselectRow:selectedDayRowinComponent:2animated:YES];
                }
            }
        }
        else
        {
            selectedYearRow = (int)row;
        }
        [birthView.datePickerselectRow:selectedYearRowinComponent:0animated:YES];
        [birthView.datePickerreloadComponent:0];
        //[birthView.datePicker reloadAllComponents];
    }
    elseif (component == 1)
    {
        //<小于当前年份,所有月份都可以选择
        if (selectedYearRow <yearRow )
        {
            selectedMonthRow = (int)row;
        }
        elseif (selectedYearRow ==yearRow)
        {
            //<=(小于等于) 当前月份,都 = 当前值
            if (row <=monthRow)
            {
                selectedMonthRow = (int)row;
            }
            else
            {
                selectedMonthRow = monthRow;
                [birthView.datePickerselectRow:selectedMonthRowinComponent:1animated:YES];
            }
        }
        //>(大于)当前年份
        else
        {
            selectedMonthRow =monthRow;
            [birthView.datePickerselectRow:selectedMonthRowinComponent:1animated:YES];
        }
        //        [birthView.datePicker selectRow:selectedMonthRow inComponent:1 animated:YES];
        [birthView.datePickerreloadComponent:1];
        //[birthView.datePicker reloadAllComponents];
    }
    elseif (component == 2)
    {
        //<小于当前年份,所有 日都可以选择
        if (selectedYearRow <yearRow )
        {
            selectedDayRow = (int)row;
        }   
        //>=(大于等于)当前年份
       elseif (selectedYearRow ==yearRow)
        {
            //<(小于) 当前月份,都可选
            if (selectedMonthRow <monthRow) {
                selectedDayRow = (int)row;
            }
            elseif (selectedMonthRow ==monthRow)
            {
                if (row > dayRow ) {
                    selectedDayRow = dayRow;
                    [birthView.datePickerselectRow:selectedDayRowinComponent:2animated:YES];
                }
                else
                {
                   selectedDayRow = (int)row;
                }
            }
            else
            {
                selectedDayRow = dayRow;
                [birthView.datePickerselectRow:selectedDayRowinComponent:2animated:YES];
            }
        }
        else
        {
            selectedDayRow =dayRow;
            [birthView.datePickerselectRow:selectedDayRowinComponent:2animated:YES];
        }
        [birthView.datePickerreloadComponent:2];
    }
}

#pragma mark - 取消,完成
- (void)click:(UIButton *)button
{
    if (button ==birthView.cancelBtn) {
        [birthView.alphaViewremoveFromSuperview];
        [birthView.backViewremoveFromSuperview];
    }
    elseif (button == birthView.finishBtn)
    {
        NSString *yearStr = [NSStringstringWithFormat:@"%@",[yearArrayobjectAtIndex:[birthView.datePickerselectedRowInComponent:0]]];
        year = [yearStrcomponentsSeparatedByString:@"年"].firstObject;
        NSString *monthStr = [NSStringstringWithFormat:@"%@",[monthArrayobjectAtIndex:[birthView.datePickerselectedRowInComponent:1]]];
        month = [monthStrcomponentsSeparatedByString:@"月"].firstObject;
        if ([monthintValue] < 10)
        {
            month = [NSStringstringWithFormat:@"0%d",[monthintValue]];
        }
        
        NSString *dayStr = [NSStringstringWithFormat:@"%@",[DaysArrayobjectAtIndex:[birthView.datePickerselectedRowInComponent:2]]];;
        day = [dayStrcomponentsSeparatedByString:@"日"].firstObject;
        if ([dayintValue] < 10)
        {
            day = [NSStringstringWithFormat:@"0%d",[dayintValue]];
        }
        //生日日期字符串
        birthString = [NSStringstringWithFormat:@"%@-%@-%@",year,month,day];
        //修改完之后,发送请求告诉修改成功
        [selfchangeBirth];
    }
}

#pragma mark- 修改生日请求
- (void)changeBirth
{
    //判断日期是否在十年之内,十年之前数据才可以请求 (< 10直接可以请求)
    if ([yearintValue] < ([currentyearStringintValue] - 10) )
    {
        //修改生日请求
        [selfchangeBirthRequest];
    }
    //== 十年时,判断月份
    else if([yearintValue] == ([currentyearStringintValue] - 10))
    {
        if ([monthintValue] < [currentMonthStringintValue])
        {
            //修改生日请求
            [selfchangeBirthRequest];
        }
        //==当前月份,判断当前日
        elseif([monthintValue] == ([currentMonthStringintValue]))
        {
            if([dayintValue] <= [currentDateStringintValue])
            {
                //修改生日请求
                [selfchangeBirthRequest];
            }
            else
            {
                //提示
                [[AlertHandlersingleton]showToastHUD:topViewmessage:@"生日需选择十年之前日期!"sleepTime:1];
            }
        }
        else
        {
            //提示
            [[AlertHandlersingleton]showToastHUD:topViewmessage:@"生日需选择十年之前日期!"sleepTime:1];
        }
    }
    else
    {
        //提示
        [[AlertHandlersingleton]showToastHUD:topViewmessage:@"生日需选择十年之前日期!"sleepTime:1];
    }
}

//修改生日请求
- (void)changeBirthRequest
{
    /**
     *  发送请求修改日期
     */
    BOOL isSuccess =YES;
    if (isSuccess)
    {
        //获取新数据并刷新table
        lbl_birth.text =birthString;
        //[self event_reloadData];
        //隐藏
        [birthView.alphaViewremoveFromSuperview];
        [birthView.backViewremoveFromSuperview];
        //提示
        [[AlertHandlersingleton]showToastHUD:topViewmessage:@"保存成功!"sleepTime:1];
    }
    else
    {
        [[AlertHandlersingleton]showToastHUD:topViewmessage:@"生日修改失败,请重试!"sleepTime:1];
    }
}

@end

 

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值