-常见的选择必须满足这两个协议,约定实施。一个为数据源协议
-托付协议负责控制控件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;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。