1、SingleViewApplication项目,拖一个pickerView控件、一个label和一个button,并且为pickerView和label定义输出口,为button添加事件触发。
2、viewController.h以及.m的代码如下,这里我们吧属性和事件触发声明都放在了头文件中,并且新建三个属性,我们这里用省份和城市来展现普通选择器,所以这三个属性分别是全部数据、当前省的数据和当前市的数据,因为之后还需要用到数据源的链接,所以还声明了委托协议和数据源协议:
//
// ViewController.h
// TestProject
//
// Created by 侯家奇 on 16/8/17.
// Copyright © 2016年 侯家奇. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) NSDictionary *pickerData; //保存全部数据
@property (nonatomic, strong) NSArray *pickerProvincesData; //当前省的数据
@property (nonatomic, strong) NSArray *pickerCitiesData; //当前市的数据
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)onclick:(id)sender;
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
@end
//
// ViewController.m
// TestProject
//
// Created by 侯家奇 on 16/8/17.
// Copyright © 2016年 侯家奇. 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 *selectedProvince = [self.pickerProvincesData objectAtIndex:0];
self.pickerCitiesData = [self.pickerData objectForKey:selectedProvince];
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)onclick:(id)sender {
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;
}
- (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];
}
}
- (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 *selectedProvince = [self.pickerProvincesData objectAtIndex:row];
NSArray *array = [self.pickerData objectForKey:selectedProvince];
self.pickerCitiesData = array;
[self.pickerView reloadComponent:1];
}
}
@end
关于数据源协议和委托协议,这里重写了四个方法,前两个是数据源协议,后两个数委托协议:
numberOfComponentsInPickerView:选择器中拨盘的数目。
pickerView:numberOfRowsInComponent:选择器中某个拨盘的行数。
pickerView:titleForRow:forComponent:为选择器中某个拨盘的行数提供显示数据。
pickerView:didSelectRow:inComponent:选中选择器的某个拨盘中的某行时调用。
4、这里我们需要一个资源文件:provinces_cities.plist可以直接用xcode新建,然后省份定义成array,就可以了,自己添加一些选项,目录结构如下:
关于数据源协议和委托协议的链接,可以通过代码的形式,也可以通过InterfaceBuilder工具,以上的代码中定义了连接
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
分别拖动即可: