datepicker重写类,只有年月两栏(鄙视下ios,丫的多个年月两栏的picker会死啊),
来源:互联网 发布:矩阵式组织结构案例 编辑:程序博客网 时间:2024/06/05 02:44
。h文件
#import <UIKit/UIKit.h>
//设置委托
@protocol MyDatepickerDelegate <NSObject>
@optional
-(void)valuechange:(NSDate *)mydate;
@end
@interface MyDatePicker :UIControl//UIViewController
<UIPickerViewDelegate,UIPickerViewDataSource>
{
UIPickerView *kengdiepicker;
// UIDatePicker *kengdiepicker;
NSDate *minimumDate,*maximumDate;
NSDate *date;
NSArray *month;
NSMutableArray *year;
int thismonth,thisyear;
id<MyDatepickerDelegate> _thedate;//设置委托变量
// UIDatePicker *picker;
}
@property(nonatomic, retain) UIPickerView *kengdiepicker;
//@property(nonatomic, retain) UIDatePicker *kengdiepicker;
@property(nonatomic, retain) NSDate *minimumDate;
@property(nonatomic, retain) NSDate *maximumDate;
@property(nonatomic, retain) NSDate *date;
@property(nonatomic, retain) NSArray *month;
@property(nonatomic, retain) NSMutableArray *year;
@property(nonatomic, assign)id<MyDatepickerDelegate> _thedate;
-(id)MyInit;
-(void)setdate:(NSDate *)mydate;
@end
。m文件//
#import "MyDatePicker.h"
@implementation MyDatePicker
@synthesize kengdiepicker;
@synthesize minimumDate;
@synthesize maximumDate;
@synthesize date;
@synthesize month;
@synthesize year;
@synthesize _thedate;
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(component==0){
return [self.monthcount];
}
if(component==1){
return [self.yearcount];
}else{
return 0;
}
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSDateFormatter *timeformatter=[[NSDateFormatteralloc]init];
[timeformatter setDateFormat:@"yyyy"];
int minyear=[[timeformatter stringFromDate:self.minimumDate]intValue]-1950;
int maxyear=[[timeformatter stringFromDate:self.maximumDate]intValue]-1950;
[timeformatter setDateFormat:@"MM"];
int thisyears=[[timeformatter stringFromDate:self.date]intValue]-1950;
int minmonth=[[timeformatter stringFromDate:self.minimumDate]intValue];
int maxmonth=[[timeformatter stringFromDate:self.maximumDate]intValue];
// if(minyear<=thisyear<=maxyear){
if(component==1){
thisyear=row;
if(thisyears>maxyear||thisyears<minyear){
if(thisyear>maxyear){
self.date=self.maximumDate;
//月份颜色
for(int i=0;i<12;i++){
if(maxmonth>i){
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}else{
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}
}
//年颜色
for(int i=0;i<100;i++){
if(i>maxyear){
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}else{
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
}
}else if(thisyear<minyear){
self.date=self.minimumDate;
for(int i=0;i<12;i++){
if(minmonth-2<i){
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}else{
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}
}
for(int i=0;i<100;i++){
if(i<minyear){
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}else{
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
}
}
}
if(minyear<thisyear&&thisyear<maxyear){
for(int i=0;i<12;i++){
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
for(int i=0;i<100;i++){
if(i<minyear||i>maxyear){
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}else{
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
}
}
if(minyear==thisyear){
for(int i=0;i<12;i++){
if(minmonth-2<i){
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}else{
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}
}
for(int i=0;i<100;i++){
if(i<minyear||i>maxyear){
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}else{
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
}
}
if(maxyear==thisyear){
for(int i=0;i<12;i++){
if(maxmonth>i){
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}else{
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}
}
for(int i=0;i<100;i++){
if(i<minyear||i>maxyear){
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}else{
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
}
}
}
if(component==0){
thismonth=row+1;
}
// }
NSDateFormatter *dateformatter=[[NSDateFormatteralloc]init];
[dateformatter setDateFormat:@"yyyy-MM"];
NSString *time=[[NSStringalloc]init];
time=[NSString stringWithFormat:@"%d-%d",thisyear+1950,thismonth];
self.date=[dateformatterdateFromString:time];
[self._thedatevaluechange:self.date];
[self.kengdiepickerreloadInputViews];
}
-(void)willMoveToWindow:(UIWindow *)newWindow{
NSDateFormatter *timeformatter=[[NSDateFormatteralloc]init];
[timeformatter setDateFormat:@"yyyy"];
int minyear=[[timeformatter stringFromDate:self.minimumDate]intValue]-1950;
int maxyear=[[timeformatter stringFromDate:self.maximumDate]intValue]-1950;
[timeformatter setDateFormat:@"MM"];
int thisyears=[[timeformatter stringFromDate:self.date]intValue]-1950;
int minmonth=[[timeformatter stringFromDate:self.minimumDate]intValue];
int maxmonth=[[timeformatter stringFromDate:self.maximumDate]intValue];
if(minyear<=thisyear<=maxyear){
if(thisyears>maxyear||thisyears<minyear){
if(thisyears>maxyear){
self.date=self.maximumDate;
//月份颜色
for(int i=0;i<12;i++){
if(maxmonth>i){
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}else{
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}
}
//年颜色
for(int i=0;i<100;i++){
if(i>maxyear){
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}else{
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
}
}else if(thisyear<minyear){
self.date=self.minimumDate;
for(int i=0;i<12;i++){
if(minmonth-2<i){
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}else{
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}
}
for(int i=0;i<100;i++){
if(i<minyear){
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}else{
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
}
}
}
if(minyear<thisyear&&thisyear<maxyear){
for(int i=0;i<12;i++){
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
for(int i=0;i<100;i++){
if(i<minyear||i>maxyear){
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}else{
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
}
}
if(minyear==thisyear){
for(int i=0;i<12;i++){
if(minmonth-2<i){
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}else{
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}
}
for(int i=0;i<100;i++){
if(i<minyear||i>maxyear){
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}else{
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
}
}
if(maxyear==thisyear){
for(int i=0;i<12;i++){
if(maxmonth>i){
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}else{
UILabel *label=[self.monthobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}
}
for(int i=0;i<100;i++){
if(i<minyear||i>maxyear){
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColorgrayColor];
}else{
UILabel *label=[self.yearobjectAtIndex:i];
label.textColor=[UIColordarkTextColor];
}
}
}
}
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
NSDateFormatter *timeformatter=[[NSDateFormatteralloc]init];
[timeformatter setDateFormat:@"yyyy"];
int minyear=[[timeformatter stringFromDate:self.minimumDate]intValue]-1950;
int maxyear=[[timeformatter stringFromDate:self.maximumDate]intValue]-1950;
[timeformatter setDateFormat:@"MM"];
if(component==0){
if(0<=row<[self.monthcount]){
UILabel *label=[self.monthobjectAtIndex:row];
[label setFrame:CGRectMake(10,0, 150, 30)];
[label setBackgroundColor:[UIColorclearColor]];
[label setFont:[UIFontfontWithName:@"Helvetica-Bold"size:25]];
return label;
}
}
if(component==1){
if(0<=row&&row<[self.yearcount]){
UILabel *label=[self.yearobjectAtIndex:row];
[label setFont:[UIFontfontWithName:@"Helvetica-Bold"size:25]];
[label setFrame:CGRectMake(10,0, 150, 30)];
if(minyear<=row&&row<=maxyear){
label.textColor=[UIColordarkTextColor];
}else{
label.textColor=[UIColorgrayColor];
}
// [myview addSubview:label];
return label;
}
}
return nil;
}
-(void)setdate:(NSDate *)mydate{
self.date=mydate;
NSDateFormatter *timeformatter=[[NSDateFormatteralloc]init];
[timeformatter setDateFormat:@"yyyy"];
thisyear=[[timeformatter stringFromDate:self.date]intValue]-1950;
[timeformatter setDateFormat:@"MM"];
thismonth=[[timeformatter stringFromDate:self.date] intValue];
[self.kengdiepickerselectRow:thisyearinComponent:1animated:YES];
[self.kengdiepickerselectRow:thismonth-1inComponent:0animated:YES];
}
-(id)MyInit{
id sender=[superinit];
if(self.month==nil){
UILabel *Januarylabel=[[UILabelalloc]init];
Januarylabel.tag=1;
Januarylabel.text=@" January";
UILabel *Februarylabel=[[UILabelalloc]init];
Februarylabel.text=@" February";
Februarylabel.tag=2;
UILabel *Marchlabel=[[UILabelalloc]init];
Marchlabel.tag=3;
Marchlabel.text=@" March";
UILabel *Aprillabel=[[UILabelalloc]init];
Aprillabel.tag=4;
Aprillabel.text=@" April";
UILabel *Maylabel=[[UILabelalloc]init];
Maylabel.tag=5;
Maylabel.text=@" May";
UILabel *Junelabel=[[UILabelalloc]init];
Junelabel.tag=6;
Junelabel.text=@" June";
UILabel *Julylabel=[[UILabelalloc]init];
Julylabel.tag=7;
Julylabel.text=@" July";
UILabel *Augustlabel=[[UILabelalloc]init];
Augustlabel.tag=8;
Augustlabel.text=@" August";
UILabel *Septemberlabel=[[UILabelalloc]init];
Septemberlabel.tag=9;
Septemberlabel.text=@" September";
UILabel *Ocoberlabel=[[UILabelalloc]init];
Ocoberlabel.tag=10;
Ocoberlabel.text=@" October";
UILabel *Novemberlabel=[[UILabelalloc]init];
Novemberlabel.tag=11;
Novemberlabel.text=@" November";
UILabel *Decemberlabel=[[UILabelalloc]init];
Decemberlabel.tag=12;
Decemberlabel.text=@" December";
self.month=[[NSArrayalloc]initWithObjects:Januarylabel,Februarylabel,Marchlabel,Aprillabel,Maylabel,Junelabel,Julylabel,Augustlabel,Septemberlabel,Ocoberlabel,Novemberlabel,Decemberlabel,nil];
// self.month=[[NSArray alloc]initWithObjects:@"January",@"February",@"March",@"April",@"May",@"June",@"July",@"August",@"September",@"Ocober",@"November",@"December", nil];
}
if(self.year==nil){
self.year=[[NSMutableArrayalloc]init];
for(int i=1950;i<2051;i++){
UILabel *label=[[UILabelalloc]init];
label.text=[NSStringstringWithFormat:@" %d",i];
label.backgroundColor=[UIColorclearColor];
[self.yearaddObject:label];
[label release];
}
}
if(self.date==nil){
self.date=[[NSDatealloc]init];
}
if(self.kengdiepicker==nil){
self.kengdiepicker=[[UIPickerViewalloc] initWithFrame:CGRectMake(0.0,0.0, 320.0,216.0)];
}
self.kengdiepicker.delegate=self;
self.kengdiepicker.showsSelectionIndicator=YES;
if(self.minimumDate==nil){
self.minimumDate=[[NSDatealloc]init];
}
if(self.maximumDate==nil){
self.maximumDate=[[NSDatealloc]init];
}
NSDate *inittime=[[NSDatealloc]init];
self.date=inittime;
NSDateFormatter *timeformatter=[[NSDateFormatteralloc]init];
[timeformatter setDateFormat:@"yyyy"];
thisyear=[[timeformatter stringFromDate:self.date]intValue]-1950;
[timeformatter setDateFormat:@"MM"];
thismonth=[[timeformatter stringFromDate:self.date] intValue];
[self.kengdiepickerselectRow:thisyearinComponent:1animated:YES];
[self.kengdiepickerselectRow:thismonth-1inComponent:0animated:YES];
NSLog(@"thisyear:%d,thismonth:%d",thisyear,thismonth);
[selfsetFrame:CGRectMake(0.0,0.0, 320.0,216.0)];
[selfaddSubview:self.kengdiepicker];
[selfbringSubviewToFront:kengdiepicker];
self.autoresizesSubviews=YES;
return sender;
}
@end
- datepicker重写类,只有年月两栏(鄙视下ios,丫的多个年月两栏的picker会死啊),
- 只有年月的输入框
- datepicker只选择年月的设置技巧
- Android 只有年月没有日的datepickerdialog
- ios 当前年月获取近半年的年月
- 基于datepicker的只选择年月的日期选择器
- jqueryui datepicker 选择中文年月时错误换行的解决办法
- extjs实现年月格式的日期选择(DatePicker)
- Android中DatePicker只显示年月的方法
- WPF 可选择年月及显示格式的DatePicker控件
- cordova-plugin-datepicker 添加只选择年月的模式(android)
- ios计算每个年月的天数
- 用Style Report做的“近两年月销售分析图”
- POJ1008My Calendar两种日历年月日间的转换月份用字符数组表示
- jsp页面显示半年内的日期,两行,上面年月,下面日
- BootStrop datepicker 选择年月
- datepicker只显示年月
- 可选年月的日历
- Linux驱动中,probe函数何时被调用
- TortoiseSVN学习笔记
- hdu 4313 Matrix
- network: android 网络判断
- WebSphere Portal V6.1 获取登录用户信息
- datepicker重写类,只有年月两栏(鄙视下ios,丫的多个年月两栏的picker会死啊),
- Activity之间的切换动画
- 查看环境变量的命令:运行-cmd-set
- Oracle 数据库实例启动关闭过程
- ios开发基础之@dynamic关键字的作用
- 最大熵模型
- JAVA操作——获取文件扩展名,去掉文件扩展名
- 指针,数组,变量参数传递的一点总结
- 世界上最简单的安装虚拟机系统的方法