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,所以必须实 ...
随机推荐
- html5+ XMLHttpRequest
XMLHttpRequest 让发送一个HTTP请求变得非常容易.你只需要简单的创建一个请求对象实例,打开一个URL,然后发送这个请求.当传输完毕后,结果的HTTP状态以及返回的响应内容也可以从请求对 ...
- 查看.ssh文件在哪
输入命令 ll -d ~/.ssh 后你就都明白了.
- kafka原理和实践(一)原理:10分钟入门
系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...
- 高频dom操作和页面性能优化(转载)
作者:gxt19940130 原文:https://feclub.cn/post/content/dom 一.DOM操作影响页面性能的核心问题 通过js操作DOM的代价很高,影响页面性能的主要问题有如 ...
- Foundation基础框架
自己总结的 // // main.m // 01-结构体 // // Created by Mac-ZhangXiaoMeng on 14/12/29. // Copyright (c) 2014年 ...
- 2017计算机学科夏令营上机考试-B编码字符串
B:编码字符串 总时间限制: 1000ms 内存限制: 65536kB 描述 在数据压缩中,一个常用的方法是行程长度编码压缩.对于一个待压缩的字符串,我们可以依次记录每个字符及重复的次数.例如,待 ...
- javascript 复制数组
常用复制数组的几种方法 直接arr1 = arr2 这种方法复制的是原数组的引用,修改复制出来的新数组会改变原来数组的内容 var arr = [1, 2, 3, 6]; var arr_ = arr ...
- 推荐一款基于bootstrap的漂亮的前端模板—inspinia_admin
首先给出Demo网址:http://cn.inspinia.cn inspinia admin 最新版 bootstrap 完全响应式后台管理模板,采用扁平化设计.使用Bootstrap 3+ Fra ...
- MFC中小笔记(三)
10.在添加新Menu之后,代码中 调用 创建的IDR_MENU1,一直出现 Debug Assertion Failed的情况.原因是,没有写入到 项目.RC中,需要更新下rc(资源文件). 然后进 ...
- Django的ModelForm组件
创建类 from django.forms import ModelForm from django.forms import widgets as wd from app01 import mode ...