UIPickerView 的使用
控件的代码与UITabelView非常类似 更简单,代理也类似
#import "ViewController.h"
@interface ViewController ()@property (nonatomic,strong)UIPickerView * pickerV;
@property (nonatomic,strong)NSMutableArray *letter;
@property (nonatomic,strong)NSMutableArray *number;
@property (nonatomic,strong)NSMutableArray *senc;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData];
int w =[UIScreen mainScreen].bounds.size.width;
int h = [UIScreen mainScreen].bounds.size.height;
int ph = 400;//_pickerV 的高
int py = 80;
_pickerV = [[UIPickerView alloc]initWithFrame:CGRectMake(0, py, w, ph)];
_pickerV.backgroundColor = [UIColor clearColor];
self.pickerV.delegate = self;
self.pickerV.dataSource = self;//设置代理
[self.view addSubview:self.pickerV];
UILabel * hour = [[UILabel alloc]initWithFrame:CGRectMake(w/6+20, (ph-py)/2+20, 50, 40)];//(ph-py)/2+20 20为每一 行的高度/2
hour.text = @"时";
hour.backgroundColor = [UIColor yellowColor];
[_pickerV addSubview:hour];
UILabel * fen = [[UILabel alloc]initWithFrame:CGRectMake(w/2+20, (ph-py)/2+20, 50, 40)];
fen.text = @"分";
fen.backgroundColor = [UIColor yellowColor];
[_pickerV addSubview:fen];
UILabel * miao = [[UILabel alloc]initWithFrame:CGRectMake(w/6*5+20, (ph-py)/2+20, 50, 40)];
miao.text = @"秒";
miao.backgroundColor = [UIColor yellowColor];
[_pickerV addSubview:miao];
UIButton * butt = [UIButton buttonWithType:0];
butt.frame = CGRectMake(0, 440, 100, 40);
butt.backgroundColor = [UIColor grayColor];
[butt addTarget:self action:@selector(clioi) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:butt];
[_pickerV selectRow:6 inComponent:0 animated:YES];
[_pickerV selectRow:6 inComponent:1 animated:YES];
[_pickerV selectRow:6 inComponent:2 animated:YES];
//NSString *seletedHeight = [ objectAtIndex:selectedHeightIndex];
//self.heightLabel.text = seletedHeight;
}
-(void)clioi//获取当前选择的
{
NSInteger sIndex = [self.pickerV selectedRowInComponent:0];
NSInteger eIndex = [self.pickerV selectedRowInComponent:1];
NSInteger lIndex = [self.pickerV selectedRowInComponent:2];
NSLog(@"%zi,%zi,%zi",sIndex,eIndex,lIndex);
}
-(void)loadData
{
self.letter = [NSMutableArray new];
self.number = [NSMutableArray new];
self.senc = [NSMutableArray new];
//需要展示的数据以数组的形式保存
for (int i = 0; i<24; i++) {
[self.letter addObject:[NSString stringWithFormat:@"%d",i]];
}
for (int i = 0; i<60; i++) {
[self.number addObject:[NSString stringWithFormat:@"%d",i]];
}
for (int i = 0; i<60; i++) {
[self.senc addObject:[NSString stringWithFormat:@"%d",i]];
}
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;//Component 数
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component//行高
{
return 40;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component //行数
{
NSInteger result = 0;
switch (component) {
case 0:
result = self.letter.count;//根据数组的元素个数返回几行数据
break;
case 1:
result = self.number.count;
break;
case 2:
result = self.senc.count;
break;
default:
break;
}
return result;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component//行内容
{
NSString * title = nil;
switch (component) {
case 0:
title = self.letter[row];
break;
case 1:
title = self.number[row];
break;
case 2:
title = self.number[row];
break;
default:
break;
}
return title;
}
@end