#import "ViewController.h"
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
{
UILabel *lable;
NSArray *array;
}
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
array=[NSArray arrayWithObjects:@"0",@"1",@"2",@"3" ,@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",nil];
UIButton *button3=[UIButton buttonWithType:UIButtonTypeCustom];
button3.backgroundColor=[UIColor greenColor];
button3.frame=CGRectMake(100, 300, 120, 50);
[button3 addTarget:self action:@selector(showMyPickerView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
}
-(void)showMyPickerView:(UIButton *)sender
{
//UIPickerView选择器的功能
UIView *whiteView=[[UIView alloc]initWithFrame:self.view.frame];
//添加视图进行遮挡
whiteView.tag=150;
whiteView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:whiteView];
//UIPickerView选择器的功能,实现数据的选择
UIPickerView *pickerView1=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 280, 300)];
pickerView1.center=whiteView.center;
pickerView1.delegate=self;
pickerView1.dataSource=self;
[whiteView addSubview:pickerView1];
UIButton *button0=[UIButton buttonWithType:UIButtonTypeCustom];
button0.frame=CGRectMake(0, 0, 80, 60);
button0.backgroundColor=[UIColor greenColor];
[button0 setTitle:@"close" forState:UIControlStateNormal];
[button0 addTarget:self action:@selector(closePickerView:) forControlEvents:UIControlEventTouchUpInside];
[whiteView addSubview:button0];
lable=[[UILabel alloc]initWithFrame:CGRectMake(100,20, 200, 40)];
lable.backgroundColor=[UIColor yellowColor];
lable.tag=160;
[whiteView addSubview:lable];
}
-(void)closePickerView:(UIButton *)sender
{
UIView *removeView=[self.view viewWithTag:150];
[removeView removeFromSuperview];//移除白色遮挡视图
}
//返回选择器的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
//返回当前显示的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return array.count;
}
//显示数组中的数字在对应的行中
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [array objectAtIndex:row];
}
//获取单元行的内容
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSString *str1=[array objectAtIndex:row];
NSString *string1=[self pickerView:pickerView titleForRow:row forComponent:0];
UILabel *getlable=(UILabel *)[self.view viewWithTag:160];
NSLog(@"%@---%@",str1,string1);
getlable.text=string1;
}