码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发--UIPickerView(选择器控件) 省份和城市的做法

时间:2016-03-11 23:49:12      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

//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(选择器控件) 省份和城市的做法

标签:

原文地址:http://www.cnblogs.com/tmf-4838/p/5267500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!