iOS通過pickerView自訂簡單的時間選取器

來源:互聯網
上載者:User
/** 初始化pickerView */-(void)initPickerView{    int  x_hour  = 50;    int  height_hour  = 120;    int  toolBar_height = 40;    float width_hour = MainScreen_Width/2 - x_hour;    float y_hour = MainScreen_Height/2 - height_hour - toolBar_height;    self.hourPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(x_hour, y_hour, width_hour, height_hour)];    self.hourPickerView.dataSource = self;    self.hourPickerView.delegate = self;    self.hourPickerView.backgroundColor = [UIColor whiteColor];    [self addSubview:self.hourPickerView];    self.minutesPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(x_hour+width_hour, y_hour, width_hour, height_hour)];    self.minutesPickerView.dataSource = self;    self.minutesPickerView.delegate = self;    self.minutesPickerView.backgroundColor = [UIColor whiteColor];    [self addSubview:self.minutesPickerView];        UIView *toolBarView = [[UIView alloc] initWithFrame:CGRectMake(x_hour,self.hourPickerView.frame.size.height+self.hourPickerView.frame.origin.y, 2*width_hour, toolBar_height)];   // toolBarView.backgroundColor = [UIColor orangeColor];    [self addSubview:toolBarView];        NSArray *btnTitleArr = [NSArray arrayWithObjects:@"取消",@"確定", nil];        for(int i=0; i < [btnTitleArr count]; i++)    {        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];        btn.frame = CGRectMake(i*toolBarView.frame.size.width/2,0, toolBarView.frame.size.width/2, toolBarView.frame.size.height);        [btn setTitle:btnTitleArr[i] forState:UIControlStateNormal];        [btn setBackgroundColor:[UIColor orangeColor]];        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];        btn.tag = 100+i;        [toolBarView addSubview:btn];        if(i == 1)        {            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(btn.frame.origin.x,btn.frame.origin.y, 0.5, btn.frame.size.height)];            lineView.backgroundColor = [UIColor grayColor];            [toolBarView addSubview:lineView];        }    }        //設定預設選擇目前時間    [self.hourPickerView selectRow:currentHour inComponent:0 animated:YES];    [self.minutesPickerView selectRow:3 inComponent:0 animated:YES];}/** 初始化資料來源 */-(void)_setArr{    hourArr = [NSMutableArray array];    mintuesArr = [NSMutableArray array];        currentHour = [[NSDate date] getHour];    timeSelect = [NSString stringWithFormat:@"%02d:%02d",[[NSDate date] getHour],30];        for(int i=0; i<24; i++)    {        [hourArr addObject:[NSString stringWithFormat:@"%d",i]];    }    for(int i=0; i<6; i++)    {        [mintuesArr addObject:[NSString stringWithFormat:@"%d",i*10]];    }}-(void)btnAction:(UIButton *)sender{    switch (sender.tag) {        case 100://取消        {            if(self.hidePickerView)            {                self.hidePickerView(sender.tag,@"");                            }        }            break;        case 101://確定        {            if(self.hidePickerView)            {                self.hidePickerView(sender.tag,timeSelect);            }        }            break;        default:            break;    }}#pragma mark - UIPickerViewDataSource- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 1;}-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{       if(pickerView == self.hourPickerView)    {        return [hourArr count];    }    else    {        return [mintuesArr count];    }}- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{    if(pickerView == self.hourPickerView)    {        selectHour = [NSString stringWithFormat:@"%02d",[hourArr[row] intValue]];    }    else if(pickerView == self.minutesPickerView)    {        selectMin = [NSString stringWithFormat:@"%02d",[mintuesArr[row] intValue]];    }    timeSelect = [NSString stringWithFormat:@"%@:%@",selectHour,selectMin];}#pragma makr - UIPickerViewDelegate- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)componen{    if(pickerView == self.hourPickerView)    {        return [hourArr objectAtIndex:row];    }    else    {        return [mintuesArr objectAtIndex:row];    }}- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated{    } 

使用方法:

 CustomDatePickView *datepickView = [[CustomDatePickView alloc] initWithFrame:self.view.bounds];           //通過block響應呼叫事件和接受選擇的時間值             __weak  CustomDatePickView *wDatePickerView = datepickView;            datepickView.hidePickerView = ^(NSInteger senderTag,NSString *selectTime){                switch (senderTag) {                    case 100:                    {                        [wDatePickerView removeFromSuperview];                    }                        break;                    case 101:                    {                        [wDatePickerView removeFromSuperview];                        selectTimeStr = selectTime;                        NSString *selectDate = @"";                        for(UIButton *btn in self.storedBtnView.subviews)                        {                            if(btn.selected)                            {                                switch (btn.tag) {                                    case 100://今天                                    {                                        selectDate = [NSString stringWithFormat:@"%d-%02d-%02d %@ %@",[dateDic[@"year"] intValue],[dateDic[@"month"] intValue],[dateDic[@"day"] intValue],[self _getWeakDay:btn],selectTime];                                                                                [self updateBookingBtnUI:selectDate];                                    }                                        break;                                    case 101://明天                                    {                                        selectDate = [NSString stringWithFormat:@"%d-%02d-%02d %@ %@",[dateDic[@"nextYear"] intValue],[dateDic[@"tomorrowMonth"] intValue],[dateDic[@"tomorrow"] intValue],[self _getWeakDay:btn],selectTime];                                        [self updateBookingBtnUI:selectDate];                                    }                                        break;                                    case 102://後天                                    {                                        selectDate = [NSString stringWithFormat:@"%d-%02d-%02d %@ %@",[dateDic[@"lastYear"] intValue],[dateDic[@"afterDayMonth"] intValue],[dateDic[@"afterDay"] intValue],[self _getWeakDay:btn],selectTime];                                        [self updateBookingBtnUI:selectDate];                                    }                                        break;                                    default:                                        break;                                }                            }                        }                    }                        break;                    default:                        break;                }            };            [self.view addSubview:datepickView];


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.