[置顶] iOS自定义日期并进行卡控
2016-05-30 17:13
721 查看
@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
相关文章推荐
- IOS 开发中的KVC 和KVO
- std::ios::sync_with_stdio(false);
- iOS Quartz2D的基本图形绘制
- iOS原生CIFilter创建二维码
- ios开发——支付宝SDK集成流程
- iOS 离屏渲染研究
- iOS CGContextRef的使用
- ios开发——CMDeviceMotion陀螺仪的使用
- iOS视图对象的生命周期
- iOS 短信验证码倒计时按钮的实现
- 正则表达式
- iOS9创建PCH文件
- iOS 开发疑难杂症
- CIFilter应用:多图叠加合成
- iOS 宏(define)与常量(const)的正确使用
- iOS 浅拷贝(Shallow Copy)与深拷贝(Deep Copy)
- 【iOS开发】获取当前view(视图)所在的Controller(控制器)
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解
- iOS 两日期之间相差几天
- 苹果开发(二) 申请应用