@interface UIPickerVC ()@property(nonatomic, strong)UILabel* showLabel;//显示所选
@property(nonatomic,strong)UIPickerView* showPickView;
@property(nonatomic,strong)NSArray* showArr;
@property (nonatomic,strong)NSString* showStr;
@end
@implementation UIPickerVC
- (void)viewDidLoad {
[super viewDidLoad];
[self initInterface];
}
#pragma mark---初始化界面
- (void)initInterface{
self.title = @"uipickerview练习";
self.view.backgroundColor = [UIColor whiteColor];
UILabel* nameLab = [[UILabel alloc]init];
[self.view addSubview:nameLab];
nameLab.frame = CGRectMake(20, 100, 80, 20);
nameLab.font = [UIFont systemFontOfSize:15];
nameLab.textAlignment = NSTextAlignmentCenter;
nameLab.textColor = [UIColor blackColor];
nameLab.text = @"所选名字:";
self.showLabel = [[UILabel alloc]init];
[self.view addSubview:self.showLabel];
self.showLabel.frame = CGRectMake(100, 100, 100, 20);
self.showLabel.font = [UIFont systemFontOfSize:15];
self.showLabel.textAlignment = NSTextAlignmentLeft;
self.showLabel.textColor = [UIColor blackColor];
self.showArr = @[@"志鹏",@"举博",@"远航"];
UIView* picvBackView = [[UIView alloc]init];
[self.view addSubview:picvBackView];
picvBackView.frame = CGRectMake(0,[UIScreen mainScreen].bounds.size.height-130, [UIScreen mainScreen].bounds.size.width, 130);
picvBackView.backgroundColor = [UIColor lightGrayColor];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[picvBackView addSubview:button];
[button setTitle:@"确定" forState:UIControlStateNormal];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-50, 10, 40, 20);
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(didClick) forControlEvents:UIControlEventTouchUpInside];
self.showLabel.text = self.showArr[0];
self.showPickView = [[UIPickerView alloc]init];
self.showPickView.frame = CGRectMake(0, 30, [UIScreen mainScreen].bounds.size.width, 90);
[picvBackView addSubview:self.showPickView];
self.showPickView.delegate = self;
self.showPickView.dataSource = self;
}
- (void)didClick{
self.showLabel.text = self.showStr;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 30;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return self.showArr.count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
return [UIScreen mainScreen].bounds.size.width;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return self.showArr[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(@"%@",self.showArr[row]);
self.showStr = self.showArr[row];
}