iOS PickerView选择视图

@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{ UIPickerView * pickerView; NSMutableArray * listPicker;
NSMutableArray * listPicker2;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. [self CreatUI]; UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect]; back.frame = CGRectMake(, , , );
back.backgroundColor = [UIColor greenColor];
[back addTarget:self action:@selector(backk) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:back]; }
-(void)backk{ [self presentViewController:[[RootViewController alloc]init] animated:YES completion:nil]; } -(void)CreatUI{ pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(, , , )]; //显示选中框
pickerView.showsSelectionIndicator = YES;
pickerView.delegate =self;
pickerView.dataSource = self; [self.view addSubview:pickerView]; listPicker = [NSMutableArray array];
listPicker2 = [NSMutableArray array]; //dataSoure
for (int i =; i<; i++) { [listPicker addObject:[NSString stringWithFormat:@"%d",i]];
[listPicker2 addObject:[NSString stringWithFormat:@"%02d",i]]; } } /////相关代理
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return ;//列数
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return listPicker.count;//每列的row个数 } //列宽
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if (component==) {
return ;
}else{
return ;
} }
//高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ if (component==) {
return ;
}else{ return ;
} }
//选中行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ } //返回当前行的内容 此处将是数组中数值添加到滚动的那个显示兰 批量操作显示的内容
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component ==) {
return [NSString stringWithFormat:@"%@ 列",[listPicker objectAtIndex:row]];
}else{ return [NSString stringWithFormat:@"%@ 个",[listPicker objectAtIndex:row]];
} }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
一、两列 独立旋转

@interface LinkageViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
UIPickerView * pickerViewv;
NSArray * listPicker;//sheng
NSArray * listPicker2;//shi
}
@property(nonatomic,strong) UILabel * label;
@end
@implementation LinkageViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self CreatUI];
UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect];
back.frame = CGRectMake(, , , );
back.backgroundColor = [UIColor greenColor];
[back addTarget:self action:@selector(backk) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:back];
}
-(void)backk{
[self presentViewController:[[RootViewController alloc]init] animated:YES completion:nil];
}
-(void)CreatUI{
pickerViewv = [[UIPickerView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, )];
//显示选中框
pickerViewv.showsSelectionIndicator = YES;
pickerViewv.delegate =self;
pickerViewv.dataSource = self;
[self.view addSubview:pickerViewv];
listPicker = [NSMutableArray array];
listPicker2 = [NSMutableArray array];
//读取数据
NSString * path = [[NSBundle mainBundle]pathForResource:@"area" ofType:@"plist"];
listPicker = [NSArray arrayWithContentsOfFile:path];
listPicker2 = [NSArray arrayWithArray:listPicker[][@"Cities"]];
//Label
//label的布局约束
self.label = [[UILabel alloc]initWithFrame:CGRectZero];
self.label.translatesAutoresizingMaskIntoConstraints = NO;
self.label.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.label.textColor = [UIColor greenColor];
self.label.font = [UIFont systemFontOfSize:];
[self.label setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:self.label];
NSArray * labelTop = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[pickerViewv]-30-[_label(50)]" options: metrics:nil views:NSDictionaryOfVariableBindings(pickerViewv,_label)];
NSArray * labelH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[_label]-20-|" options: metrics:nil views:NSDictionaryOfVariableBindings(_label)];
[self.view addConstraints:labelTop];
[self.view addConstraints:labelH];
}
/////相关代理
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return ;//列数
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
NSInteger rows =;
switch (component) {
case :
rows = listPicker.count;
break;
case :
rows = listPicker2.count;
default:
break;
}
return rows;
}
//列宽
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
if (component==) {
return ;
}else{
return ;
}
}
//高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
if (component==) {
return ;
}else{
return ;
}
}
//选中行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (component==) {
listPicker2 = listPicker[row][@"Cities"];
[pickerViewv reloadComponent:];//刷新第几个
}else{
self.label.text = [NSString stringWithFormat:@"%@%@",listPicker[[pickerViewv selectedRowInComponent: ]][@"State"],listPicker2[[pickerViewv selectedRowInComponent:]][@"city"]];
}
}
//返回当前行的内容 此处将是数组中数值添加到滚动的那个显示兰 批量操作显示的内容
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component ==) {
return listPicker[row][@"State"];
}else{
return listPicker2[row][@"city"];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
二、第二列随着第一列刷新

@interface CustomViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{ UIPickerView * pickerView; NSMutableArray * listPicker;
NSMutableArray * listPicker2;
} @end @implementation CustomViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self CreatUI];
UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect]; back.frame = CGRectMake(, , , );
back.backgroundColor = [UIColor greenColor];
[back addTarget:self action:@selector(backk) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:back]; }
-(void)backk{ [self presentViewController:[[RootViewController alloc]init] animated:YES completion:nil]; } -(void)CreatUI{ pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height-)]; //显示选中框
pickerView.showsSelectionIndicator = YES;
pickerView.delegate =self;
pickerView.dataSource = self; [self.view addSubview:pickerView]; listPicker = [NSMutableArray array];
listPicker2 = [NSMutableArray array]; //dataSoure
for (int i =; i<; i++) { [listPicker addObject:[NSString stringWithFormat:@"%d",i]];
[listPicker2 addObject:[NSString stringWithFormat:@"%02d",i]]; } } /////相关代理
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return ;//列数
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return listPicker.count;//每列的row个数 } //列宽
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if (component==) {
return ;
}else{
return ;
} }
//高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ if (component==) {
return ;
}else{ return ;
} }
//选中行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component==) {
//选中第一列。一般刷新第二列 // [pickerViewv reloadComponent:1];//刷新第几个 }else{
//选中第二列。一般输出 } }
#pragma mark 给pickerview设置字体大小和颜色等
-(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ //可以自定义label
UILabel * pickerlabel = (UILabel*)view;
if (!pickerlabel) { pickerlabel = [[UILabel alloc] init];
pickerlabel.adjustsFontSizeToFitWidth = YES;
pickerlabel.textAlignment = NSTextAlignmentCenter;
[pickerlabel setBackgroundColor:[UIColor lightGrayColor]];
[pickerlabel setFont:[UIFont systemFontOfSize:]]; } pickerlabel.text = [self pickerView:pickerView titleForRow:row forComponent:component]; return pickerlabel; } //返回当前行的内容 此处将是数组中数值添加到滚动的那个显示兰 批量操作显示的内容
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component ==) {
return [NSString stringWithFormat:@"%@ 列",[listPicker objectAtIndex:row]];
}else{ return [NSString stringWithFormat:@"%@ 个",[listPicker objectAtIndex:row]];
} }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
三、自定义显示View
iOS PickerView选择视图的更多相关文章
- iOS开发系列--视图切换
概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...
- 浅谈iOS中的视图优化
引言: 让我们来思考几个问题,你开发过的产品,它还有可以优化的地方吗?能增加它的帧率吗?能减少多余的CPU计算吗?是不是存在多余的GPU渲染?业务这点工作量对于越来越强大的设备面前显得微不足道,但作为 ...
- Swift - iOS中各种视图控制器(View Controller)的介绍
在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来 ...
- Xamarin iOS教程之视图显示图像
Xamarin iOS教程之视图显示图像 Xamarin iOS显示图像 在主视图中显示一个图像,可以让开发者的应用程序变的更有趣,例如,在一些应用程序开始运行时,都会通过图像来显示此应用程序的玩法或 ...
- IOS弹出视图 LewPopupViewController
LewPopupViewController是一款IOS弹出视图软件.iOS 下的弹出视图.支持iPhone/iPad. 软件截图 使用方法 弹出视图 1 2 3 4 5 PopupView *vie ...
- IOS 如何选择delegate、notification、KVO?
IOS 如何选择delegate.notification.KVO? 博客分类: IOS 前面分别讲了delegate.notification和KVO的实现原理,以及实际使用步骤,我们心中不禁有 ...
- iOS开发中视图控制器ViewControllers之间的数据传递
iOS开发中视图控制器ViewControllers之间的数据传递 这里我们用一个demo来说明ios是如何在视图控制器之间传递重要的参数的.本文先从手写UI来讨论,在下一篇文章中讨论在storybo ...
- Xamarin.IOS之多视图
欢迎大家加入以下开源社区 Xamarin-Cn:https://github.com/Xamarin-Cn Mvvmcross-Cn:https://github.com/Mvvmcross-Cn ...
- (三十一)PickerView自定义视图
例如选择国家,左边是名称右边是国家,不应该使用两列,而是自定义PickerView的一列,可以通过xib来实现. 注意,虽然PickerView也是一列,但是数据源方法是@required,所以必须实 ...
随机推荐
- mysql 查询数据库内各表的占用大小
select TABLE_NAME, concat(truncate(data_length/1024/1024,2),' MB') as data_size, concat(truncate(ind ...
- 安卓自定义控件(五)触控基础MotionEvent
之前去面试,人家说,我这个事件拦截机制写得太少了,还有一个MotionEvent没写,这个确实也很重要,后来我考虑了一下,决定将这篇文章放到自己定义控件里. 先简单再提一下事件分发,事件分发和拦截主要 ...
- C语言之浮点数
#include<stdio.h> int main(){printf("请分别输入身高的英尺和英寸," "如输入\"5 7\"表示5英尺 ...
- Python中的冒泡排序
冒泡排序 冒泡排序(英语:Bubble Sort)是一种简单的排序算法.它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.遍历数列的工作是重复地进行直到没有再需要交换,也 ...
- LINQ学习系列-----3.1 查询非泛型集合
一.问题起源 LINQ to object在设计时,是配合IEnumerable<T>接口的泛型集合类型使用的,例如字典.数组.List<T>等,但是对于继承了IEnumera ...
- oracle触发器 调用 web接口
最近要求开发当数据表发生变化的时候调用web接口的需求,上网找了好几篇文章看着都觉得不是很好,也根据别人的思路去实现了下,感觉都不太理想,最后使用URLConnection实现了调用.具体查看一下代码 ...
- HiveSchemaTool-Parsing failed. Reason- Unrecognized option- -dbType mysql
版本: Hive2.1 在linux上部署Hive的时候,初始化元数据的时候,出现HiveSchemaTool:Parsing failed. Reason: Unrecognized option: ...
- Web前端学习——HTML
HTML其实还是蛮容易学习的,无非就是一些标签.格式的填写,大学的时候也做过网站设计,所以这里主要记录一些常用的HTML标签.属性以及书写方法等. 一.常见HTML格式 主要包含文件type,html ...
- C++ vector 常用API
vector: 向量容器,动态数组,类模板 定义和初始化: vector<T> v1; //v1是空vector,元素类型是T类型,执行默认初始化,int为0,string为空串 vect ...
- java学习笔记—集合之Map集合
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 12.0px Times } p.p2 { margin: 0.0p ...