效果图

#import "ViewController.h"

@interface ViewController ()
@property(strong,nonatomic)UIPickerView *myPickView1;
@property(strong,nonatomic)NSArray *guo;
@property(strong,nonatomic)NSMutableArray *sheng;
@property(strong,nonatomic)NSMutableArray *shixian;
@property(strong,nonatomic)NSDictionary *shi;
@property(strong,nonatomic)UIButton *btn;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.sheng=[NSMutableArray array];
self.shixian=[NSMutableArray array];
// self.guo=[NSArray array]; //添加UIPickerView
self.myPickView1=[[UIPickerView alloc]initWithFrame:CGRectMake(10, 100, 400, 300)];
self.myPickView1.backgroundColor=[UIColor colorWithRed:0.945 green:1.000 blue:0.359 alpha:1.000];
self.myPickView1.delegate=self;
self.myPickView1.dataSource=self; //读取plist文件
NSBundle *bundle=[NSBundle mainBundle];
NSString *path=[bundle pathForResource:@"area.plist" ofType:nil];
self.guo=[NSArray arrayWithContentsOfFile:path]; //遍历 省
for (NSDictionary *arr in self.guo) {
[self.sheng addObject:arr[@"State"]];
} [self.view addSubview:self.myPickView1]; } #pragma mark - 数据源
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2;
} #pragma mark - 数据源,返回长度
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component==FIRST) {
return self.sheng.count;
}
return self.shixian.count;
} #pragma mark - 显示信息
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component==FIRST) {
return [self.sheng objectAtIndex:row];
} return [self.shixian objectAtIndex:row]; } #pragma mark - 选中选的信息
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component==FIRST) {
//清除上次选择的城市
[self.shixian removeAllObjects];
//更具城市的索引,遍历串城市
NSDictionary *a=[self.guo objectAtIndex:row];
//遍历城市信息
NSArray *array=a[@"Cities"];
NSMutableArray *s=[NSMutableArray array];
for (NSDictionary *a in array) {
[s addObject:a[@"city"]];
}
//把值传给 self.shixian
self.shixian=s; //把第一个加到第二个中
[self.myPickView1 selectRow:row inComponent:FIRST animated:YES]; //刷新组件
[self.myPickView1 reloadComponent:SECOND]; }
//如果选中第二个,弹出信息
if (component==SECOND) {
NSInteger firstRow=[self.myPickView1 selectedRowInComponent:FIRST];
NSInteger secondRow=[self.myPickView1 selectedRowInComponent:SECOND]; NSString *firstString=[self.sheng objectAtIndex:firstRow];
NSString *secondString=[self.shixian objectAtIndex:secondRow]; NSString *message=[NSString stringWithFormat:@"您确定要选择%@%@吗?",firstString,secondString];
UIAlertController *alertMessage=[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; [alertMessage addAction:cancle];
[alertMessage addAction:ok];
[self presentViewController:alertMessage animated:YES completion:nil];
}
} #pragma mark - 行高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 30;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

ViewController.h文件

#import <UIKit/UIKit.h>
#define FIRST 0
#define SECOND 1
@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource> @end

注:plist文件结构

iOS UIPickerView 显示全国省市的更多相关文章

  1. 全国省市级联数据sql语句 mysql版

    全国省市级联数据sql语句 mysql版 --省级 provincial create table provincial ( provincialID int, provincialName ), p ...

  2. IOS 状态栏 显示与隐藏网络活动状态

    IOS中显示和隐藏状态栏的网络活动标志 //在向服务端发送请求状态栏显示网络活动标志: [[UIApplication sharedApplication] setNetworkActivityInd ...

  3. vue中解决时间在ios上显示NAN的问题

    最近在用vue,遇到倒计时在ios上显示为NAN的问题. 因为做的是倒计时支付,思路是获取服务器时间和下单时间,再转成秒级时间戳做差值. 在网上找到说是ios 不支持例如2018-09-01 10:0 ...

  4. iOS - UIPickerView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIPickerView : UIView <NSCoding, UITa ...

  5. ios日期显示NaN

    ios中js通过getMonth()获取到的日期显示NaN,而在其他地方如pc.安卓都是ok的,这是为什么呢,原来这里有个ios的兼容问题,需要将日期中的“-”替换为“/” var time = ne ...

  6. iOS:UIPickerView选择器的使用

    通过UIPickerView选择器做的一个类似于密码锁的日期时间表 源码如下: #import <UIKit/UIKit.h> @interface ViewController : UI ...

  7. ios上传图片显示方向错误问题

    IOS 上传图片方向显示错误问题 问题描述 在使用苹果手机上传图片的时候,发现传完的图片显示出来方向是错误的,竖着的图片会变成横着显示(少部分安卓手机也存在这个问题) 产生原因 ios 相机加入了方向 ...

  8. iOS 状态栏显示/隐藏

    iOS 10为止,将在plist文件中将 View controller-based status bar appearance 设置为NO,就可以用UIApplication设置状态栏. 注意,要在 ...

  9. 全国省市区县和乡镇街道行政区划矢量边界坐标经纬度地图最新数据免费下载 支持shp geojson json sql格式

    关键词: 省市区三级, 乡镇四级, 全国, 行政区划, 坐标边界, 矢量数据, 地理围栏, 免费下载, 2018 2019 2020 2021 2022年份, 最新数据, 长期更新, 开源维护, 支持 ...

随机推荐

  1. vue新建项目

    一直都被如何用vue.js新建一个项目的问题困扰着,经过好久的实践,终于搞清楚如何用vue新建项目了: 1.官网对于vue-cli介绍: Vue.js provides an official CLI ...

  2. TEA,XXTEA介绍,对称加密

    总结:在使用加密的时候,我们可以加入随机数,这样相同的明文,每次加密后得到不同的密文,同时可以在密文中加入密文有效期,控制密文的有效时间长度. 针对有的功能扩展使用,很好的思想. TEA对 64 位数 ...

  3. 图片上传预览 (URL.createObjectURL)

    知识预备:1. URL.createObjectURL() 静态方法会创建一个 DOMString,它的 URL 表示参数中的对象.这个 URL 的生命周期和创建它的窗口中的 document 绑定. ...

  4. span 元素无法设置宽度问题

    span 元素为行内元素,没有width属性,需要转换为块级元素才可以设置width: 拓展:html元素分为块级元素,行内元素.可变元素. 行内元素与块级元素直观上的区别 1 .行内元素会在一条直线 ...

  5. Yii2.0数据库操作增删改查详解

    1.简单查询: one(): 根据查询结果返回查询的第一条记录. all(): 根据查询结果返回所有记录. count(): 返回记录的数量. sum(): 返回指定列的总数. average():  ...

  6. 项目中常用的linux命令

    1. du -sm * | sort -n 把当前目录下的文件(或目录)按大小排序,看下哪个地方占用最多: 2. pwd                     获取当前目录路径 3. tail -f ...

  7. Emacs 常用快捷键

    关闭欢迎界面:Q 保存:c-x c-s 退出:c-x c-c 暂时退出:c-z       屏幕滚动 移动到下一屏:c-v 移动到上一屏:m-v 将光标所在行置中:c-L       光标移动 上一行 ...

  8. DotNetBar for Windows Forms 11.8.0.8冰河之刃重打包版

    关于 DotNetBar for Windows Forms 11.8.0.8_冰河之刃重打包版 基于 官方原版的安装包 + http://www.cnblogs.com/tracky 提供的补丁DL ...

  9. AngularJs项目实践总结

    今年3月接触AngularJs,并且在6月的项目中开始应用,从踩坑到填坑花了不少时间,根据项目中的实际应用情况总结了一些经验,如下: 一.UI控件选择 Angularjs是不缺控件的,Github里现 ...

  10. Percona Server 5.6.33-79.0 发布

    Percona Server 5.6.33-79.0 发布了,该版本基于 MySQL 5.6.33,包含了所有的 bug 修复,是Percona Server 5.6 系列中的正式版本.该版本主要是修 ...