-常见的选择必须满足这两个协议,约定实施。一个为数据源协议

-托付协议负责控制控件UI、事件响应,
实现可选

-数据源协议负责控件与应用数据模型的桥梁,一般必须实现

@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>

//
// ViewController.h
// PickViewSample
//
// Created by 李亚坤 on 14-10-21.
// Copyright (c) 2014年 李亚坤. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
// 普通选择器必须满足这两个协议,一个为托付协议,一个为数据源协议
// 托付协议负责控制控件UI、事件响应, 实现可选
// 数据源协议负责控件与应用数据模型的桥梁,一般必须实现
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView; @property (weak, nonatomic) IBOutlet UILabel *label; @property (nonatomic, strong) NSDictionary *pickerData; // 保存所有数据
@property (nonatomic, strong) NSArray *pickerProvincesData; // 当前省数据
@property (nonatomic, strong) NSArray *pickerCitiesData; // 当前省以下的市数据 - (IBAction)onclick:(id)sender;
@end

//
// ViewController.m
// PickViewSample
//
// Created by 李亚坤 on 14-10-21.
// Copyright (c) 2014年 李亚坤. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 找到文件夹指定文件并返回文件路径
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"provinces_cities"
ofType:@"plist"]; // 获取属性列表文件里的所有数据
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile: plistPath];
self.pickerData = dict; // 省份名
self.pickerProvincesData = [self.pickerData allKeys]; // 默认取出第一个省的所有市的数据
NSString *seletedProvince = [self.pickerProvincesData objectAtIndex: 0];
// objectforkey: 找到索引的数据
self.pickerCitiesData = [self.pickerData objectForKey:seletedProvince];
 
    self.pickView.dataSource = self;
    self.pickView.delegate = self;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)onclick:(id)sender {
// 设置选定的行索引从0開始
NSInteger row1 = [self.pickerView selectedRowInComponent:0];
NSInteger row2 = [self.pickerView selectedRowInComponent:1];
NSString *selected1 = [self.pickerProvincesData objectAtIndex:row1];
NSString *selected2 = [self.pickerCitiesData objectAtIndex:row2]; NSString *title = [[NSString alloc] initWithFormat:@"%@, %@市", selected1, selected2];
self.label.text = title;
} #pragma mark 实现协议UIpickerViewDataSource方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
// 返回选择器中拨轮的数目
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
// 返回选择当中某个拨轮的行数
if (component == 0) {
// 返回省的个数
return [self.pickerProvincesData count];
}
else
{
// 返回市的个数
return [self.pickerCitiesData count];
}
} #pragma mark 实现协议UIPickerViewDelegate方法
// 为选择器中某个拨轮的行提供显示数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
// 选择省的名
return [self.pickerProvincesData objectAtIndex:row];
}
else
{
return [self.pickerCitiesData objectAtIndex:row];
}
}
// 当选中某个拨轮中的行时,调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) {
NSString *seletedProvince = [self.pickerProvincesData objectAtIndex:row];
NSArray *array = [self.pickerData objectForKey:seletedProvince];
self.pickerCitiesData = array;
// 刷新
[self.pickerView reloadComponent:1];
}
} @end

关于托付和数据源的实现对象ViewController分配给UIPickerView的托付属性Delegate和数据源属性dataSource;

这一部分能够在InterfaceBuilder里面实现。分配会自己主动生成

-(void) viewDidLoad

{

...

self.pickerView.dataSource = self;

self.pickerView.delegate = self;

}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

常见的选择&lt;数据源协议,委托协议&gt;(IOS发展)的更多相关文章

  1. 【IOS 开发】Objective - C 面向对象高级特性 - 包装类 | 类处理 | 类别 | 扩展 | 协议 | 委托 | 异常处理 | 反射

    一. Objective-C 对象简单处理 1. 包装类 (1) 包装类简介 NSValue 和 NSNumber : -- 通用包装类 NSValue : NSValue 包装单个 short, i ...

  2. 动态选路、RIP协议&&OSPF协议详解

    动态选路.RIP协议&&OSPF协议详解 概念 当相邻路由器之间进行通信,以告知对方每个路由器当前所连接的网络,这时就出现了动态选路.路由器之间必须采用选路协议进行通信,这样的选路协议 ...

  3. http协议tcp协议ip协议三次握手四次挥手,为什么三次握手,为什么四次挥手,sockete套接字理解

    1.1 TCP是什么? TCP是Tranfer Control Protocol的简称,TCP协议是一种面向连接的.可靠的.基于字节流的运输层通信协议.通过TCP协议传输,得到的是一个顺序的无差错的数 ...

  4. Zookeeper——一致性协议:Zab协议

    Reference: https://www.jianshu.com/p/2bceacd60b8a 什么是Zab协议 Zab 协议的作用 Zab 协议原理 Zab 协议核心 Zab 协议内容 原子广播 ...

  5. 网络协议-webService协议

    webservice 协议 Web Service使用的是 SOAP (Simple Object Access Protocol)协议soap协议只是用来封装消息用的.封装后的消息你可以通过各种已有 ...

  6. OSI七层协议&TCP协议(三次握手四次挥手)

    今日内容 python 基础回顾 软件开发架构 网络理论前戏 OSI 七层协议(五层) TCP协议 三次握手与四次挥手 UDP协议 内容详细 一.python 基础回顾 1.基本数据类型 整型 int ...

  7. WLAN协议相关协议

    802.11协议 CAPWAP协议: RFC5415:https://tools.ietf.org/html/rfc5415 DTLS握手处理流程: RFC4347DTLS 认证: RFC5246 U ...

  8. 老李分享:HTTP协议之协议头

    老李分享:HTTP协议之协议头   当我们打开一个网页时,浏览器要向网站服务器发送一个HTTP请求头,然后网站服务器根据HTTP请求头的内容生成当次请求的内容发送给浏览器.你明白HTTP请求头的具体含 ...

  9. BOOTPROTO=[none|static|bootp|dhcp](引导时不使用协议|静态分配|BOOTP协议|DHCP协议)

     BOOTPROTO=[none|static|bootp|dhcp](引导时不使用协议|静态分配|BOOTP协议|DHCP协议) 

随机推荐

  1. js如何生成[n,m]的随机数

    js如何生成[n,m]的随机数 一.总结 一句话总结:生成随机数就是用的Math的random方法. 1.Math.random()得到的数据的左右开闭情况是怎样的? 左闭又开 所以Math.floo ...

  2. 服务器负载均衡lvs(Linux Virtual Server)

    服务器负载均衡lvs(Linux Virtual Server) 一.总结 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统. 三.Linu ...

  3. thinkphp3.2 图片平均颜色值

    public function imgColor($imgUrl) { $imageInfo = getimagesize($imgUrl); //图片类型 $imgType = strtolower ...

  4. Lucene学习总结之七:Lucene搜索过程解析 2014-06-25 14:23 863人阅读 评论(1) 收藏

    一.Lucene搜索过程总论 搜索的过程总的来说就是将词典及倒排表信息从索引中读出来,根据用户输入的查询语句合并倒排表,得到结果文档集并对文档进行打分的过程. 其可用如下图示: 总共包括以下几个过程: ...

  5. 段的创建表user_segments 分类: H2_ORACLE 2013-08-10 11:13 714人阅读 评论(0) 收藏

    1.段的定义及类型 Oracle中的段(segment)是占用磁盘空间的一个对象,最常见的段类型包括: l  聚簇cluster l  表table l  表分区 tablepartition l  ...

  6. solaris 11 stdio.h: No such file or directory

    http://www.zendo.name/solaris-11-stdio-h%EF%BC%9A-no-such-file-or-directory/ Posted on 2012 年 3 月 23 ...

  7. C - The C Answer (2nd Edition) - Exercise 1-6

    /* Verify that the expression getchar() != EOF is 0 or 1. */ #include <stdio.h> main() { int c ...

  8. [Git] Use git add --patch for better commit history and mitigating bugs

    Let's split our changes into separate commits. We'll be able to check over our changes before stagin ...

  9. .net core 下的分布式事务锁

    原文:.net core 下的分布式事务锁 目录 系统分布式锁的用法 锁的实现 锁的使用 API内的范例: 引用链接 系统分布式锁的用法 公司框架新增功能分布式锁: 锁的性能之王: 缓存 > Z ...

  10. 2015年工作中遇到的问题:81-90,标题党-Nginx与真实IP-Mybatis等

     81."标题党"与"百度收录"问题. 很久以来,就发现那些"标题党"的收录和排名情况非常好,比如CSDN某篇文章,就随便一写,就排在了第一 ...