码迷,mamicode.com
首页 > 其他好文 > 详细

uipickerview用法

时间:2016-03-17 16:06:03      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

//获取国家区号视图

@interface PickerView : UIView

@property(nonatomic,retain)RegisterViewController * registerVC;

@property (nonatomic, strong)void (^resultBlock)(NSMutableArray *resultArr);

 

#pragma  mark================获取国家区号视图

@interface PickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

{

    UIView *_pickerBg;

    UIPickerView *_picker;

    UILabel *_timeLable;

    UIButton *_cancelBtn;

    UIButton *_sureBtn;

}

@property (nonatomic, retain)NSMutableArray *selectArr;

@end

 

@implementation PickerView

 

- (id)init

{

    self = [super initWithFrame:[UIScreen mainScreen].bounds];

    if (self) {

        self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0];

        _selectArr = [NSMutableArray arrayWithArray:@[@"中国", @"+86"]];

        [self setupPickerView];

    }

    return self;

}

 

- (void)setupPickerView

{

    _pickerBg = [[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, screenWidth, 216)];

    [self addSubview:_pickerBg];

    

    _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 216)];

    _picker.backgroundColor=[UIColor whiteColor];

    _picker.delegate=self;

    _picker.dataSource=self;

    [_pickerBg addSubview:_picker];

    

    _timeLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 43.5)];

    _timeLable.backgroundColor=[UIColor whiteColor];

    _timeLable.text=@"请选择国家区号:";

    _timeLable.textColor=[UIColor darkGrayColor];

    _timeLable.font=[UIFont systemFontOfSize:17.0f];

    [_picker addSubview:_timeLable];

    

    UILabel *bottomLine=[[UILabel alloc] initWithFrame:CGRectMake(0, _timeLable.bottom, screenWidth, 0.5)];

    bottomLine.backgroundColor=[UIColor colorWithWhite:0.8 alpha:1];

    [_picker addSubview:bottomLine];

    

    _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    _sureBtn.frame=CGRectMake(screenWidth/2.0, _picker.height-44, screenWidth/2.0, 44);

    _sureBtn.backgroundColor=[UIColor colorWithWhite:0.95 alpha:1];

    _sureBtn.titleLabel.font=[UIFont systemFontOfSize:16.0f];

    [_sureBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];

    [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];

    [_sureBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

    [_pickerBg addSubview:_sureBtn];

    

    _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    _cancelBtn.frame=CGRectMake(0, _picker.height-44, screenWidth/2.0, 44);

    _cancelBtn.backgroundColor=[UIColor colorWithWhite:0.95 alpha:1];

    _cancelBtn.titleLabel.font=[UIFont systemFontOfSize:16.0f];

    [_cancelBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];

    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];

    [_cancelBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

    [_pickerBg addSubview:_cancelBtn];

    

    bottomLine=[[UILabel alloc] initWithFrame:CGRectMake(0, _sureBtn.top-0.5, screenWidth, 0.5)];

    bottomLine.backgroundColor=[UIColor colorWithWhite:0.8 alpha:1];

    [_picker addSubview:bottomLine];

    

    UILabel *vLine=[[UILabel alloc] initWithFrame:CGRectMake(screenWidth/2.0, _picker.height-44, 0.5, 44)];

    vLine.backgroundColor=[UIColor colorWithWhite:0.8 alpha:1];

    [_pickerBg addSubview:vLine];

}

 

- (void)layoutSubviews

{

    [super layoutSubviews];

    

    [UIView animateWithDuration:0.2 animations:^{

        _pickerBg.top=screenHeight-216;

        self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];

    }];

}

 

#pragma mark ------- Action

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [self hideSelfView];

}

 

- (void)btnAction:(UIButton *)btn

{

    if (_sureBtn == btn) {

        _resultBlock(_selectArr);

    }

    [self hideSelfView];

}

 

/** 隐藏视图 */

- (void)hideSelfView

{

    [UIView animateWithDuration:0.2 animations:^{

        _pickerBg.top=screenHeight;

        self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0];

    }completion:^(BOOL finished) {

        [self removeFromSuperview];

    }];

}

 

#pragma mark----UIspickerview delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 1;

}

 

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

    if (!self.registerVC.nationarArr) {

        return 0;

    }

    return [self.registerVC.nationarArr count];

}

 

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

{

    _selectArr = [NSMutableArray arrayWithArray:self.registerVC.nationarArr[row]];

}

 

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

{

    return [NSString stringWithFormat:@"%@ %@", self.registerVC.nationarArr[row][0], self.registerVC.nationarArr[row][1]] ;

}

#pragma mark----textfield delegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    [UIView animateWithDuration:0.5 animations:^{

        if (_picker.top==screenHeight) {

            _picker.frame=CGRectMake(0,screenHeight-216, screenWidth, 216);

            [self  bringSubviewToFront:_picker];

        }

    }];

    return NO;

}

 

@end

 

-(void)telNumberClick

{

    PickerView *data = [[PickerView alloc] init];

    data.registerVC=self;

    //设置_tel区号

    data.resultBlock = ^(NSMutableArray *arr){

        _tel.text=arr[1];

    };

    [[UIApplication sharedApplication].delegate.window addSubview:data];

}

 

uipickerview用法

标签:

原文地址:http://www.cnblogs.com/momosmile/p/5287527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!