您的位置:首页 > 产品设计 > UI/UE

UIPickerView

2015-10-12 16:18 696 查看
  简单的介绍一下UIPickerView,下面以单个选择器举例。

#import "ViewController.h"

//设置UIPickerView的协议

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property(nonatomic,strong) UIPickerView *pickerView;

@property(nonatomic,strong) NSArray *testArr;

@property(nonatomic,strong) UILabel *textLabel;

@end

@implementation ViewController

-(NSArray *)testArr {

if (!_testArr) {

_testArr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25"];

}

return _testArr;

}

- (void)viewDidLoad {

[super viewDidLoad];

//显示指定项的内容

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.bounds.size.width - 200)/2, 100, 200, 40)];

self.textLabel = label;

[self.view addSubview:label];

//弹出选择器

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

button.frame = CGRectMake((self.view.bounds.size.width - 100)/2, 200, 100, 40);

button.backgroundColor = [UIColor redColor];

[button setTitle:@"hehe" forState:UIControlStateNormal];

[button addTarget:self action:@selector(clickingButton) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

}

- (void)clickingButton {

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, self.view.bounds.size.height - 250, self.view.bounds.size.width - 40, 250)];

self.pickerView = pickerView;

pickerView.showsSelectionIndicator = YES;

pickerView.dataSource = self;

pickerView.delegate = self;

[self.view addSubview:pickerView];

}

//设定选择器的个数

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

return 1;

}

//指定每个选择器中选项数

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

return self.testArr.count;

}

//指定每个选项的内容

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

NSString *str = self.testArr[row];

return str;

}

//选定指定项之后的操作

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

self.textLabel.text = self.testArr[row];

[self.pickerView removeFromSuperview];

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: