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,所以必须实 ...
随机推荐
- Winwos Server 2012发布ASP.NET MVC5 项目
一.本文实验环境: Windows Server 2012 R2 Visual Studio 2015 项目为:ASP.NET MVC 5.0,使用的是SQL SERVER 2008 R2数据库 二. ...
- js 图片转换为base64
<input id="file" type="file"> <img id="img" style="max-h ...
- 一些常用软件的静默安装参数(nsis,msi,InstallShield,Inno)
打包的时候,经常需要安装一些其它的环境库,而又不想让用户繁锁的去选择,这时就需要静默安装,而不同的文件所加的参数了不一致,比如VS的环境库vcredist_x86.exe(这是32位的环境库)后面加/ ...
- 十一、Hadoop学习笔记————数据库与数据仓库
数据仓库是集成的面向主题的数据库的集合 面向主题主要是宏观上解决某一类问题,集合性指数据集 数据库主要处理用于事务处理,数据仓库用于分析处理,数据库适用于操作型数据,便于增删改查, 数据仓库则用于挖掘 ...
- Redis在本地测试没有问题,上传的服务器后出现错误
在服务器上,new Redis 可以拿到对象数据,但是其他操作就会报错. Redis 开启过程中,遇到错误 . :( protocol error, got 'S' as reply type byt ...
- C语言之scanf
#include<stdio.h>int main(){int num;int a,b,c,result,d,result1;scanf("int%d",&nu ...
- 在centOS上搭建wordpress博客系统
一.主要内容 1.安装LAMP服务器系统(Linux.Apache.MySQL.PHP ); 2.安装wordpress: 二.具体步骤 一.LAMP环境设置 1.安装LAMP系统,在centOS上可 ...
- 51Nod--1018排序
1018 排序 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出N个整数,对着N个整数进行排序 Input 第1行:整数的数量N(1 <= N ...
- 非常棒的教程记录(UML)
这里暂且记录下看过的非常棒的博客吧! 来自 CSDN 几年前的博客专栏了,我只想说:经典实用的知识永远不会过时! http://blog.csdn.net/column/details/umlmode ...
- git上传本地文件到gitlab
The repository for this project is empty If you already have files you can push them using command l ...