首页 > 其他 > 详细

UIPickerView

时间:2015-10-12 17:06:27      阅读:239      评论:0      收藏:0      [点我收藏+]

  简单的介绍一下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

 

UIPickerView

原文:http://www.cnblogs.com/yyt-hehe-yyt/p/4871994.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!