iOS开发--UIPickerView(选择器控件) 省份和城市的做法
Posted 唐唐_010
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发--UIPickerView(选择器控件) 省份和城市的做法相关的知识,希望对你有一定的参考价值。
//UIPickerView 是一个选择器控件,它可以生成单列的选择器,也可生成多列的选择器
@interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
@property(strong,nonatomic) UIPickerView *MyPickerView;
@property(strong,nonatomic) NSMutableArray *ProvinceArr;
@property(strong,nonatomic) NSMutableArray *CityArr;
@property(strong,nonatomic) NSArray *arr;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.ProvinceArr=[NSMutableArray array];
self.CityArr=[NSMutableArray array];
//文件的路径
NSString *path=[[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];
self.arr=[NSArray arrayWithContentsOfFile:path];
//省份的遍历循环
for (int i=0;i<self.arr.count; i++)
{
[self.ProvinceArr addObject:self.arr[i][@"State"]];
}
//选择器控件
self.MyPickerView=[[UIPickerView alloc] initWithFrame:CGRectMake(70, 100, 300, 300)];
self.MyPickerView.delegate=self;
self.MyPickerView.dataSource=self;
self.MyPickerView.backgroundColor=[UIColor clearColor];
[self.view addSubview:self.MyPickerView];
}
#pragma mark 数据源 numberOfComponentsInPickerView
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
#pragma mark 数据源 numberOfRowsInComponent
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component==0)
{
return self.ProvinceArr.count;
}
else
{
return self.CityArr.count;
}
}
#pragma delegate 显示信息方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component==0)
{
return self.ProvinceArr[row];
}
return self.CityArr[row];
}
#pragma mark 选中行信息
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component==0)
{
[self.CityArr removeAllObjects];
NSInteger RowProvinceArr=[pickerView selectedRowInComponent:0];
NSArray *arr11=self.arr[RowProvinceArr][@"Cities"];
//市区的遍历循环
for (int i=0; i<arr11.count; i++)
{
[self.CityArr addObject:arr11[i][@"city"]];
}
//刷新 reload
[self.MyPickerView reloadComponent:1];
}
else
{
// UIAlertView *MyAlertView=[[UIAlertView alloc] initWithTitle:@"系统提示" message:[NSString stringWithFormat:@"%@--%@",[_ProvinceArr objectAtIndex:row],[_CityArr objectAtIndex:row]]delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
// [MyAlertView show];
NSString *str=[NSString stringWithFormat:@"%@--%@",_ProvinceArr[[pickerView selectedRowInComponent:0]],_CityArr[row]];
//提示框
UIAlertController *MyAlertController=[UIAlertController alertControllerWithTitle:@"系统提示" message:str preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *MyAlertAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
NSLog(@"确定");
}];
UIAlertAction *MyAlertAction1=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
NSLog(@"取消");
}];
[MyAlertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField)
{
textField.backgroundColor=[UIColor whiteColor];
}];
[MyAlertController addAction:MyAlertAction1];
[MyAlertController addAction:MyAlertAction];
[self presentViewController:MyAlertController animated:YES completion:nil];
}
}
#pragma mark 显示行高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50.00;
}
以上是关于iOS开发--UIPickerView(选择器控件) 省份和城市的做法的主要内容,如果未能解决你的问题,请参考以下文章