- #import
- //定义两个常量,用来表示2个组件
- #define PickerOne 0
- #define PickerTwo 1
- //添加UIPickerView的委托方法和数据源方法
- @interface DoublePickerViewViewController : UIViewController
- {
- //定义输出口,字典,数组
- UIPickerView *picker;
- NSDictionary *zidian;
- NSArray *city;
- NSArray *zips;
- }
- @property (nonatomic ,retain) IBOutlet UIPickerView *picker;
- @property (nonatomic ,retain) NSDictionary *zidian;
- @property (nonatomic ,retain) NSArray *city;
- @property (nonatomic ,retain) NSArray *zips;
- //定义按钮点击时候触发的代码
- - (IBAction) buttonPressed:(id)sender;
- @end
.m文件
- #import "DoublePickerViewViewController.h"
- @implementation DoublePickerViewViewController
- @synthesize picker;
- @synthesize zidian;
- @synthesize city;
- @synthesize zips;
- //按钮触发代码
- - (IBAction) buttonPressed:(id)sender
- {
- //询问选取器选中了哪个行
- NSInteger cityRow = [picker selectedRowInComponent:PickerOne];
- NSInteger zipsRow = [picker selectedRowInComponent:PickerTwo];
- //返回选中行的值
- NSString *cityName = [self.city objectAtIndex:cityRow];
- NSString *zipsName = [self.zips objectAtIndex:zipsRow];
- //把选中的值分配给字符串
- NSString *title = [[NSString alloc] initWithFormat:@"你选择的邮编是%@",zipsName];
- NSString *message = [[NSString alloc] initWithFormat:@"%@是在%@里的邮编",
- zipsName,cityName];
- //定义一个警告窗口
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
- cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [alert show];
- [alert release];
- [title release];
- [message release];
- }
- - (void)viewDidLoad
- {
- //提取对应用程序主束的引用,主要是为了获取添加在Resources的资源
- NSBundle *bundle = [NSBundle mainBundle];
- //获取code.plist的路径
- NSString *listPath = [bundle pathForResource:@"code" ofType:@"plist"];
- //用获取到的路径创建一个字典文件
- NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:listPath];
- //分配给zidian
- self.zidian = dictionary;
- [dictionary release];
- //获取字典里所有的数组
- NSArray *components = [self.zidian allKeys];
- //按照字母顺序排序
- NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
- //然后分配给city
- self.city = sorted;
- //提取索引为0的city行,也就是第一行
- NSString *selectedCity = [self.city objectAtIndex:0];
- //用这个返回的字符串提取对应的zips数组
- NSArray *array = [zidian objectForKey:selectedCity];
- //将它分配给zips
- self.zips = array;
- [super viewDidLoad];
- }
- - (void)viewDidUnload
- {
- self.picker = nil;
- self.zidian = nil;
- self.city = nil;
- self.zips = nil;
- [super viewDidUnload];
- }
- - (void)dealloc {
- [picker release];
- [zidian release];
- [city release];
- [zips release];
- [super dealloc];
- }
- #pragma mark -
- #pragma mark picker 数据源方法
- //选取器如果有多个滚轮,就返回滚轮的数量,我们这里有两个,就返回2
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- {
- return 2;
- }
- //返回给定的组件有多少行数据,我们有2个组件,所以用if操作。后期tableview也是类似的操作方法
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- {
- if (component == PickerOne) {
- return [self.city count];
- }
- return [self.zips count];
- }
- #pragma mark -
- #pragma mark picker 委托方法
- //官方的意思是,指定组件中的指定数据,我理解的就是目前选中的是哪一行。
- - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
- forComponent:(NSInteger)component
- {
- if (component == PickerOne) {
- return [self.city objectAtIndex:row];
- }
- return [self.zips objectAtIndex:row];
- }
- //当选取器的行发生改变的时候调用这个方法
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
- inComponent:(NSInteger)component
- {
- //如果第一个滚轮发生改变,开始一下方法。
- if (component == PickerOne) {
- //选中了哪个城市
- NSString *selectedCity = [self.city objectAtIndex:row];
- //在字典里找到对应的邮编
- NSArray *array = [zidian objectForKey:selectedCity];
- //分配给zips
- self.zips = array;
- //右侧的邮编,默认停留在第一行,0表示第一行。
- [picker selectRow:0 inComponent:PickerTwo animated:YES];
- //重新加载第二个滚轮。
- [picker reloadComponent:PickerTwo];
- }
- }
- @end