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


原创粉丝点击