UIPickerView的基本使用

可以这样写一个基于UIView的分类

#import

typedefvoid(^pickerSeletedEndBlock)(idresult);

@interfaceXHWPickerView :UIView

@property (retain, nonatomic) NSArray *pickerDataArr;

@property (retain, nonatomic) UIPickerView *pickerView;

@property (nonatomic, copy) pickerSeletedEndBlock selectEndBlock;

- (void)popPickerView;

@end

#import "XHWPickerView.h"

#define XHW_W [UIScreen mainScreen].bounds.size.width

#define XHW_H [UIScreen mainScreen].bounds.size.height

#define PICKERVIEW_H300

@interface XHWPickerView()

{

    NSIntegerselectRow;

}

@property (retain, nonatomic) UIView *baseView;

@end

@implementation XHWPickerView

- (id)initWithFrame:(CGRect)frame

{

    self= [superinitWithFrame:frame];

    if(self) {


        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];

        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, XHW_H-PICKERVIEW_H, XHW_W, XHW_H)];

        _baseView.backgroundColor = [UIColor whiteColor];

        [selfaddSubview:_baseView];


        UIButton*btnOK = [[UIButtonalloc]initWithFrame:CGRectMake(XHW_H-50,0,40,40)];

        [btnOKsetTitle:@"确定" forState:UIControlStateNormal];


        [btnOKsetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [btnOKaddTarget:self action:@selector(pickerViewBtnOK:) forControlEvents:UIControlEventTouchUpInside];

        [_baseViewaddSubview:btnOK];


        UIButton*btnCancel = [[UIButtonalloc]initWithFrame:CGRectMake(10,0,40,40)];

        [btnCancelsetTitle:@"取消" forState:UIControlStateNormal];

        [btnCancelsetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [btnCanceladdTarget:self action:@selector(pickerViewBtnCancel:) forControlEvents:UIControlEventTouchUpInside];


        UIView*lineVie = [[UIViewalloc]init];

        [_baseViewaddSubview:lineVie];

        lineVie.backgroundColor= [UIColorgrayColor];

        lineVie.alpha=0.2;

        [lineViemas_makeConstraints:^(MASConstraintMaker*make) {

            make.left.right.offset(0);

            make.top.equalTo(btnOK.mas_bottom).offset(-1);

            make.height.offset(1);

        }];

        [_baseViewaddSubview:btnCancel];

        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, XHW_W, PICKERVIEW_H-40)];

        _pickerView.delegate=self;

        _pickerView.dataSource=self;

        _pickerView.backgroundColor = [UIColor whiteColor];

        [_baseView addSubview:_pickerView];

        UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(dismissPickerView)];

        [selfaddGestureRecognizer:tapGesture];

    }

    return self;

}

#pragma mark - UIPickerViewDataSource

//返回多少列

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

    return 1;

}

//每列对应多少行

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

    return _pickerDataArr.count;

}

//每行显示的数据

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

    return _pickerDataArr[row];

}

#pragma mark - UIPickerViewDelegate

//选中pickerView的某一行

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

{

 selectRow= row;

}

#pragma mark - Private Menthods

//弹出pickerView

- (void)popPickerView

{

    [UIView animateWithDuration:0.5

                     animations:^{

                         self.frame=CGRectMake(0,0,XHW_W,XHW_H);

                     }];

}

//取消pickerView

- (void)dismissPickerView

{

    [UIView animateWithDuration:0.5

                     animations:^{

                         self.frame=CGRectMake(0,XHW_H,XHW_W,XHW_H);

                     }];

}

//确定

- (void)pickerViewBtnOK:(id)sender

{

    if (self.selectEndBlock) {

        self.selectEndBlock(_pickerDataArr[selectRow]);

    }

    [self dismissPickerView];

}

//取消

- (void)pickerViewBtnCancel:(id)sender

{

    if (self.selectEndBlock) {

        self.selectEndBlock(nil);

    }

    [self dismissPickerView];


}

外界使用时

 YCPickerView *pickView = [[YCPickerView alloc] init];

    pickView.arrPickerData = self.pickerData;

    [pickView.pickerView selectRow:2 inComponent:0 animated:YES];

    [self.viewaddSubview:pickView];

    pickView.frame=self.view.bounds;

    [pickViewpopPickerView];

    pickView.selectBlock= ^(NSString*str) {

        //这里可以拿到选中后的数据

        NSLog(@"%@",str);


    };

你可能感兴趣的:(UIPickerView的基本使用)