uipickerview用法

//获取国家区号视图

@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];

[email protected]"请选择国家区号:";

_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];

}

时间: 2024-07-29 15:53:13

uipickerview用法的相关文章

UIPickerView用法(左右比例,整体大小,字体大小)

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero]; pickerView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; //这里设置了就可以自定                                                                  

iOS开发UI篇—UIPickerView控件简单介绍

iOS开发UI篇—UIPickerView控件简单介绍 一.UIPickerView 控件 1.简单介绍: 2.示例代码 TXViewController.m文件 1 // Created by 鑫 on 14-10-15. 2 3 // Copyright (c) 2014年 梁镋鑫. All rights reserved. 4 5 // 6 7 8 9 #import "TXViewController.h" 10 11 12 13 @interface TXViewContro

猫猫学IOS(二十)UI之UIPickerView_点菜系统

猫猫分享,必须精品 素材代码地址:http://blog.csdn.net/u013357243/article/details/45057267 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 先看效果图 ## UIPickerView控件 UIPickerView用处: 用来展示很多行(row) 很多列(component )的数据,多用于电子商务的点菜,城市选择等等. UIPickerV

UIPickerView基本用法

#import "ViewController.h" #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource> { UILabel *lable; NSArray *array; } @end @interface ViewController () @end @implementation V

UIPickerView的使用(一)

简介:UIPickerView是一个选择器控件,它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活.UIPickerView直接继承了UIView,没有继承UIControl,因此,它不能像UIControl那样绑定事件处理方法,UIPickerView的事件处理由其委托对象完成.使用UIPickerView的对象应该遵守UIPickerViewDataSource,UIPickerViewDelegate.

UIPickerView(选择器)

UIPickerView也是一个选择器控件,它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活. UIPickerView直接继承了UIView,没有继承UIControl,因此,它不能像UIControl那样绑定事件处理方法,UIPickerView的事件处理由其委托对象完成. UIPickerView控件常用的属性和方法如下. Ø numberOfComponents:获取UIPickerView指定列中

用UIPickerView来显示省和市

UIPickerView 是一个选择器控件, 它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活.UIPickerView 直接继承了 UIView ,没有继承 UIControl ,因此,它不能像 UIControl 那样绑定事件处理方法, UIPickerView 的事件处理由其委托对象完成. 正文: UIPickerView 控件常用的属性和方法如下:   numberOfComponents: 获取UIPickerView指定列中包含的列

iOS开发——高级UI之OC篇&amp;UIdatePicker&amp;UIPickerView简单使用

UIdatePicker&UIPickerView简单使用 /***********************************************************************************/ 一:UIdatePicker:(日期控件) 1.UIDatePicker什么时候用? 当用户选择日期的时候,一般弹出一个UIDatePicker给用户选择. 2.UIDatePickerios6和ios7/8的区别 下面看看使用封装的代码怎么去实现它: 因为这个比较简

iOS开发日记48-详解UIPickerView

今天博主有一个UIPickerView的需求,遇到了一些困难点,在此和大家分享,希望能够共同进步. UIPickerView是一个选择器控件,它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活. UIPickerView直接继承了UIView,没有继承UIControl,因此,它不能像UIControl那样绑定事件处理方法,UIPickerView的事件处理由其委托对象完成. self.viewOfPick=