博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS中UIPickerView实现省/市连动
阅读量:5239 次
发布时间:2019-06-14

本文共 4118 字,大约阅读时间需要 13 分钟。

UIPickerView实现省/市的连动

1 #import 
2 3 @interface ViewController : UIViewController
4 @property(strong,nonatomic) UIPickerView *myPickView; 5 @property(strong,nonatomic) NSArray *array;//用于存储数据 6 @property(strong,nonatomic) NSMutableArray *provinceArray;//省份数组 7 @property(strong,nonatomic) NSMutableArray *cityArray;//城市数组 8 @property(strong,nonatomic) UIAlertController *alterController; 9 @property(strong,nonatomic) UIAlertAction *actionA;10 @property(strong,nonatomic) UIAlertAction *actionB;11 12 @end
1 #import "ViewController.h" 2  3 @interface ViewController () 4  5  6 @end 7  8 @implementation ViewController 9 10 - (void)viewDidLoad {11     [super viewDidLoad];12     //初始化13     self.provinceArray=[NSMutableArray array];14     self.cityArray=[NSMutableArray array];15     16     self.myPickView=[[UIPickerView alloc] initWithFrame:CGRectMake(30, 100, 350, 200)];17     self.myPickView.backgroundColor=[UIColor greenColor];18     //指定代理19     self.myPickView.delegate=self;20     self.myPickView.dataSource=self;21     22     [self.view addSubview:self.myPickView];23     24     //提取文件25     NSString *path=[[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];26     self.array=[NSArray arrayWithContentsOfFile:path];27     28     //把地区数据提取出来存储到 dic 字典中29     for (NSDictionary *dic in self.array) {30         //把字典 dic 中的省提取出来放到集合 provinceArray 中31         [self.provinceArray addObject:dic[@"State"]];32     }33     //NSLog(@"%@",self.provinceArray);34     35     36     37     38 }
1 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 2 { 3     return 2; //输出的是两列 4 } 5  6 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 7 { 8     if (component==0) { 9         return self.provinceArray.count;10     }else11     {12         return self.cityArray.count;13     }14 }15 16 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component17 {18     if (component==0) {19         return [self.provinceArray objectAtIndex:row];20     }21     return [self.cityArray objectAtIndex:row];22 }23 24 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component25 {26     if (component==0)27     {28         //清除上次选择的城市29         [self.cityArray removeAllObjects];30         NSDictionary *dic=[self.array objectAtIndex:row];31         NSArray *array1=dic[@"Cities"];32         NSMutableArray *cityA=[NSMutableArray array];33         for (NSDictionary *tempdic in array1) {34             [cityA addObject:tempdic[@"city"]];35         }36         self.cityArray=cityA;37         [self.myPickView selectRow:row inComponent:0 animated:YES];38         //刷新组件39         [self.myPickView reloadComponent:1];40     }41     if(component==1){42         43         NSInteger integerFirst=[self.myPickView selectedRowInComponent:0];44         NSInteger integerTow=[self.myPickView selectedRowInComponent:1];45         NSString *firstStr=[self.provinceArray objectAtIndex:integerFirst];46         NSString *towStr=[self.cityArray objectAtIndex:integerTow];47         NSString *str=[NSString stringWithFormat:@"您要选择的是:%@, %@吗?",firstStr,towStr];48         self.alterController=[UIAlertController alertControllerWithTitle:@"系统提示" message:str preferredStyle:UIAlertControllerStyleAlert];49         self.actionA=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];50         self.actionB=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];51         [self.alterController addAction:self.actionA];52         [self.alterController addAction:self.actionB];53         [self presentViewController:self.alterController animated:YES completion:nil];54     }55 56 }57 58 -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component59 {60     return 40;61 }62 - (void)didReceiveMemoryWarning {63     [super didReceiveMemoryWarning];64     // Dispose of any resources that can be recreated.65 }

 

转载于:https://www.cnblogs.com/zhaochaobin/p/5267555.html

你可能感兴趣的文章
用PHP向数据库中实现简单的增删改查(纯代码)
查看>>
Android调用WCF
查看>>
微信支付问题
查看>>
使用ConfuserEx加密混淆程序以及如何脱壳反编译
查看>>
C++结构体的初始化问题
查看>>
spark DataSetHolder.toDF 的问题
查看>>
自定义搜索程序
查看>>
Codeforces.1110E.Magic Stones(思路 差分)
查看>>
【转】Unity3.5 GameCenter基础教程
查看>>
[原]IOS 设备基本信息
查看>>
java 中使用log4j
查看>>
时钟效果
查看>>
C#中使用goto
查看>>
NSData转NSString
查看>>
分享Kali Linux 2016.2第49周虚拟机
查看>>
Xamarin Android项目真机测试闪退
查看>>
(转)C# 泛型详解
查看>>
Excel公式巧用
查看>>
expect实现配置机器信任关系
查看>>
0821: aniy hadoop 1-6的步骤安装总结。。我怕自己忘记
查看>>