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
#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
相关文章推荐
- The following classes could not be found - EditText (Change to android.widget.EditText, Fix Build Pa
- UINavigationController和页面间传值
- Java设计模式 —— 建造模式 (Builder)
- NSIS 的 Modern UI 教程(二)
- UI:使View背景逐渐变暗的方法
- BlueDroid 蓝牙启动流程分析
- Hdu 5496 Beauty of Sequence (组合数)
- zoj 3911 Prime Query (ZOJ Monthly, October 2015 - I)
- 关于UITableView的section最后一个分割线的优化
- easyui表单提交,后台获取不到值
- When START-OF-SELECTION is Not Required
- Request获取url各种信息的方法
- iOS开发笔记-Autolayout自动布局与UIView动画
- id to load is required for loading----id被要求加载exception
- Flume学习9_log4j:ERROR setFile(null,true) call failed产生的一个原因
- UI-UIView
- The request sent by the client was syntactically incorrect (). 的某个解决办法
- jsp中国文字形式提交,request对象获取乱码
- iOS仿京东分类菜单之UICollectionView内容
- UVA-1612 Guess (贪心)