UIPickerView做好的时间选择
来源:互联网 发布:数据分析师是不是很累 编辑:程序博客网 时间:2024/05/16 10:59
//
// PickerView.m
// text
//
// Created by Apple on 2017/4/21.
// Copyright © 2017年 Apple. All rights reserved.
// 万能的时间选择器
#import "PickerView.h"#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)#define BACKGROUND_BLACK_COLOR [UIColor colorWithRed:0.412 green:0.412 blue:0.412 alpha:0.7]static const int pickerViewHeight = 228;static const int toolBarHeight = 44;@interface PickerView()<UIPickerViewDelegate,UIPickerViewDataSource>@property(nonatomic,strong)QQStrResultBlock block;@property(nonatomic,assign)NSInteger component;@property(nonatomic,assign)NSInteger typeID;@property(nonatomic,strong)NSArray *yearArray;@property(nonatomic,strong)NSArray *monthArray;@property(nonatomic,strong)NSArray *dayArray;@property(nonatomic,strong)NSArray *weakArray;@property(nonatomic,strong)NSString *pickerViewRowStr;@property(nonatomic,strong)NSString *componentStr1;@property(nonatomic,strong)NSString *componentStr2;@property(nonatomic,strong)NSString *componentStr3;@end@implementation PickerView+(void)showPickerView:(UIView *)view componentNum:(NSInteger)num typePickerID:(NSInteger)typeID selectStr:(NSString *)selectStr StrBlock:(QQStrResultBlock)strBlock{ PickerView *pickerView = [[PickerView alloc]initWithFrame:view.bounds]; pickerView.component = num; pickerView.typeID = typeID; pickerView.block = strBlock; [pickerView typeJudge]; pickerView.componentStr1 = pickerView.yearArray[0]; pickerView.componentStr2 = pickerView.monthArray[0]; if (pickerView.typeID == 3) { pickerView.componentStr3 = pickerView.dayArray[0]; }else{ pickerView.componentStr2 = pickerView.weakArray[0]; } [pickerView initView]; if (pickerView.typeID == 1) { pickerView.pickerViewRowStr = [NSString stringWithFormat:@"%@",pickerView.yearArray[0]]; }else if (pickerView.typeID == 2){ pickerView.pickerViewRowStr = [NSString stringWithFormat:@"%@,%@",pickerView.yearArray[0],pickerView.monthArray[0]]; }else if (pickerView.typeID == 3){ pickerView.pickerViewRowStr = [NSString stringWithFormat:@"%@,%@,%@",pickerView.yearArray[0],pickerView.monthArray[0],pickerView.dayArray[0]]; }else{ pickerView.pickerViewRowStr = [NSString stringWithFormat:@"%@,%@",pickerView.yearArray[0],pickerView.weakArray[0]]; } [view addSubview:pickerView];}-(void)initView{ UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - pickerViewHeight, SCREEN_WIDTH, pickerViewHeight)]; containerView.backgroundColor = [UIColor whiteColor]; UIButton *btnOK = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -70, 5, 40, 30)]; btnOK.titleLabel.font = [UIFont systemFontOfSize:18.0]; [btnOK setTitle:@"确定" forState:UIControlStateNormal]; [btnOK setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [btnOK addTarget:self action:@selector(pickerViewBtnOk:) forControlEvents:UIControlEventTouchUpInside]; [containerView addSubview:btnOK]; UIButton *btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(30, 5, 40, 30)]; btnCancel.titleLabel.font = [UIFont systemFontOfSize:18.0]; [btnCancel setTitle:@"取消" forState:UIControlStateNormal]; [btnCancel setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [btnCancel addTarget:self action:@selector(pickerViewBtnCancel:) forControlEvents:UIControlEventTouchUpInside]; [containerView addSubview:btnCancel]; UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 32, SCREEN_WIDTH, pickerViewHeight - toolBarHeight)]; pickerView.backgroundColor = [UIColor whiteColor]; pickerView.delegate = self; pickerView.dataSource = self; [containerView addSubview:pickerView]; self.backgroundColor = BACKGROUND_BLACK_COLOR; [self addSubview:containerView];}- (void)pickerViewBtnOk:(UIButton *)btn{ if (self.typeID == 1) { self.pickerViewRowStr = [NSString stringWithFormat:@"%@",self.componentStr1]; self.block(self.pickerViewRowStr); }else if (self.typeID == 2){ self.pickerViewRowStr = [NSString stringWithFormat:@"%@,%@",self.componentStr1,self.componentStr2]; self.block(self.pickerViewRowStr); }else if (self.typeID == 3){ self.pickerViewRowStr = [NSString stringWithFormat:@"%@,%@,%@",self.componentStr1,self.componentStr2,self.componentStr3]; self.block(self.pickerViewRowStr); }else{ self.pickerViewRowStr = [NSString stringWithFormat:@"%@,%@",self.componentStr1,self.componentStr2]; self.block(self.pickerViewRowStr); } [self removeFromSuperview];}- (void)pickerViewBtnCancel:(UIButton *)btn{ [self removeFromSuperview];}#pragma mark - PickerDataSource- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return self.component*2;}- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (self.typeID == 1) { if (component == 0) { return self.yearArray.count; }else{ return 1; } }else if (self.typeID == 2){ if (component == 0) { return self.yearArray.count; }else if(component == 1){ return 1; }else if (component == 2){ return self.monthArray.count; }else{ return 1; } }else if (self.typeID == 3){ if (component == 0) { return self.yearArray.count; }else if (component == 1){ return 1; }else if(component == 2){ return self.monthArray.count; }else if(component == 3){ return 1; }else if (component == 4){ return self.dayArray.count; }else{ return 1; } }else{ if (component == 0) { return self.yearArray.count; }else if(component == 1){ return 1; }else if (component == 2){ return self.weakArray.count; }else{ return 1; } }}#pragma mark - PickerDelegate- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return 44;}- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (self.typeID == 1) { if (component == 0) { _componentStr1 = self.yearArray[row]; } }else if (self.typeID == 2){ if (component == 0) { _componentStr1 = self.yearArray[row]; }else if (component == 2){ _componentStr2 = self.monthArray[row]; } }else if (self.typeID == 3){ if (component == 0) { _componentStr1 = self.yearArray[row]; }else if (component == 2){ _componentStr2 = self.monthArray[row]; }else if (component == 4){ _componentStr3 = self.dayArray[row]; } }else{ if (component == 0) { _componentStr1 = self.yearArray[row]; }else if (component == 2){ _componentStr2 = self.weakArray[row]; } }![这里写图片描述](http://img.blog.csdn.net/20170421150941360?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG92ZWlvc29sb3ZlaHVh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)}- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ if (self.typeID == 1) { if (component == 0) { NSString *str = self.yearArray[row]; return [self singleLine:pickerView rowStr:str]; }else{ NSString *str = @"年"; return [self singleLine:pickerView rowStr:str]; } }else if (self.typeID == 2) { if (component == 0) { NSString *str =self.yearArray[row]; return [self singleLine:pickerView rowStr:str]; }else if(component == 1){ NSString *str = @"年"; return [self singleLine:pickerView rowStr:str]; }else if (component == 2) { NSString *str = self.monthArray[row]; return [self singleLine:pickerView rowStr:str]; }else{ NSString *str = @"月"; return [self singleLine:pickerView rowStr:str]; } }else if (self.typeID == 3) { if (component == 0) { NSString *str = self.yearArray[row]; return [self singleLine:pickerView rowStr:str]; }else if(component == 1){ NSString *str = @"年"; return [self singleLine:pickerView rowStr:str]; }else if (component == 2) { NSString *str = self.monthArray[row]; return [self singleLine:pickerView rowStr:str]; }else if(component == 3){ NSString *str = @"月"; return [self singleLine:pickerView rowStr:str]; }else if (component == 4) { NSString *str = self.dayArray[row]; return [self singleLine:pickerView rowStr:str]; }else{ NSString *str = @"日"; return [self singleLine:pickerView rowStr:str]; } }else{ if (component == 0) { NSString *str = self.yearArray[row]; return [self singleLine:pickerView rowStr:str]; }else if (component == 1){ NSString *str = @"年"; return [self singleLine:pickerView rowStr:str]; }else if (component == 2){ NSString *str = self.weakArray[row]; return [self singleLine:pickerView rowStr:str]; }else{ NSString *str = @"周"; return [self singleLine:pickerView rowStr:str]; } }}-(UILabel *)singleLine:(UIPickerView *)pickerView rowStr:(NSString *)rowStr{ //设置分割线的颜色 for(UIView *singleLine in pickerView.subviews){ if (singleLine.frame.size.height < 1){ singleLine.backgroundColor = [UIColor redColor]; } } //设置文字的属性 UILabel *genderLabel = [UILabel new]; genderLabel.textAlignment = NSTextAlignmentCenter; genderLabel.text = rowStr; genderLabel.font = [UIFont systemFontOfSize:15]; genderLabel.textColor = [UIColor blackColor]; return genderLabel;}-(void)typeJudge{ if (self.typeID == 1) { self.yearArray = @[@"2015",@"2016",@"2017",@"2018",@"2019",@"2020",@"2021"]; }else if (self.typeID == 2){ self.yearArray = @[@"2015",@"2016",@"2017",@"2018",@"2019",@"2020",@"2021"]; self.monthArray = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"]; }else if (self.typeID == 3){ self.yearArray = @[@"2015",@"2016",@"2017",@"2018",@"2019",@"2020",@"2021"]; self.monthArray = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"]; self.dayArray = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31"]; }else if (self.typeID == 4){ self.yearArray = @[@"2015",@"2016",@"2017",@"2018",@"2019",@"2020",@"2021"]; self.weakArray = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52"]; }}@end
0 0
- UIPickerView做好的时间选择
- UIPickerView 双排选择时间点
- IOS UIPickerView控件的关联选择
- 数据选择控件-UIPickerView的总结
- UIPickerView选择一个默认的值
- Swift - 选择框(UIPickerView)的用法
- 利用uipickerview获取时间的倒计时程序
- UIPickerView(滚动选择控制器)
- iOS uipickerview 选择DATE
- UIPickerView + 城市选择
- UIPickerView城市选择
- [IOS]UIPickerView选择时间段
- UIPickerView - 城市选择
- UIPickerView选择省市
- UIPickerView(选择框)
- iOS学习之UIPickerView控件的关联选择
- iOS学习之UIPickerView控件的关联选择
- iOS学习之UIPickerView控件的关联选择
- Hibernate学习-13:操作持久化对象的方法
- mxnet系列之 c++11
- 基于Platform7.0.1 Coframe实现的JSP权限按钮组件
- 复数的运算
- HTTPS之SSL/TLS协议分析
- UIPickerView做好的时间选择
- 关于tshark的configuration profile
- springboot返回jsp页面
- spring学习笔记 --配置项(一)
- bzoj 2534: Uva10829L-gap字符串 (后缀数组+ST表)
- linux下gcc-编译静态库
- Eclipse中jsp、js文件编辑时,卡死现象解决汇总
- Xcode 快捷键
- Java不定长参数