Package your own Uipickerview plus a time frame

Source: Internet
Author: User

1. Add a proxy:

@interface Viewcontroller () <UIPickerViewDataSource,UIPickerViewDelegate>

2. Add storage arrays and Views:

View

@property (Nonatomic,strong) Uipickerview *pickerview;

Date
@property (Nonatomic,strong) Nsmutablearray *daydatearray;

Disappear
@property (Nonatomic,strong) Nsmutablearray *hoursdatearray;


3 Creating a View:

-(void) creatbtn{UIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake (30, 30, 100, 30)];    [Btn settitle:@ "click" Forstate:uicontrolstatenormal];    [Btn Settintcolor:[uicolor Blackcolor];    [Btn Setbackgroundcolor:[uicolor Redcolor];    [Btn addtarget:self Action: @selector (btnaction:) forcontrolevents:uicontroleventtouchupinside]; [Self.view addsubview:btn];} -(void) Btnaction: (UIButton *) sender{[self creatpicker];}    -(void) creatpicker{nsstring *strvalue;  for (int i = 0; i < i++) {if (i + 1 <) {strvalue = [nsstring stringwithformat:@ "0%d:00-        0%d:00 ", I, i + 1];        }else if (i + 1 = = Ten) {strvalue = [nsstring stringwithformat:@ "0%d:00-%d:00", I, i + 1]; }else if (i + 1>10 && i + 1 <) {strvalue = [nsstring stringwithformat:@ "%d:00-%d:00", I, i +        1];        }else if (i+1 = =) {strvalue = [nsstring stringwithformat:@ "%d:00-00:00", I]; } [SELF.HOURSDAtearray Addobject:strvalue];    } Nsinteger datemonth = 0;        Nsinteger dateday = 0;                for (int i = 0; i<; i++) {NSDate *date = [NSDate datewithtimeintervalsincenow:i* (24*60*60)];        Nscalendar *calendar = [Nscalendar Currentcalendar]; nsdatecomponents *comps = [Calendar components: (Nscalendarunitmonth |        Nscalendarunitday) fromdate:date];        Datemonth = [Comps month];        Dateday = [Comps day];        NSString *datevaule;        if (i = = 0) {Datevaule = @ "Today";        }else{datevaule = [NSString stringwithformat:@ "%ld month%ld Day", Datemonth,dateday];    } [Self.daydatearray Addobject:datevaule];    } Self.pickerview = [[Uipickerview alloc] Initwithframe:cgrectmake (50, 50, 200, 200)];    Self.pickerView.autoresizingMask = Uiviewautoresizingflexiblewidth;    Self.pickerView.dataSource = self;    Self.pickerView.delegate = self;    Self.pickerView.showsSelectionIndicator = YES; [Self.view Addsubview: Self.pickerview];} 

Override protocol Method:

-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{return 2;}         -(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component{if (component = = 0) {    return self.dayDateArray.count;    }else{return self.hoursDateArray.count; }}-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component    Reusingview: (UIView *) view{UILabel *myview = nil;                if (component = = 0) {MyView = [[UILabel alloc] initwithframe:cgrectmake (0.0, 0.0, 100, 30)];                Myview.textalignment = Nstextalignmentcenter;                Myview.text = [Self.daydatearray objectatindex:row];         Myview.font = [Uifont systemfontofsize:17];    Use label to set font size Myview.backgroundcolor = [Uicolor Clearcolor];                }else{MyView = [[UILabel alloc] initwithframe:cgrectmake (0.0, 0.0, 150, 30)]; Myview.text = [Self.hoursdatearray ObjectatindeX:row];                Myview.textalignment = Nstextalignmentcenter;                Myview.font = [Uifont systemfontofsize:17];    Myview.backgroundcolor = [Uicolor Clearcolor]; } return MyView;} -(CGFloat) Pickerview: (Uipickerview *) Pickerview widthheightforcomponent: (Nsinteger) component{if (component = = 0)    {return 100;    }else{return 150; }}-(CGFloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (nsinteger) component{return 30;} -(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component{if (sel F.daydatearray.count-1>=row && component = 0) && (self.hoursdatearray.count-1>= row && component = = 1) {NSString *strvaule = [NSString stringwithformat:@ "%@%@", [Self.daydatearray objectatindex:row],[self    . Hoursdatearray Objectatindex:row-2]];    NSLog (@ "%@", strvaule); }}

Effect:


Package your own Uipickerview plus a time frame

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.