• 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;

    }

  • 相关阅读:
    白鸦:互联网就是社区,液态的社区
    「芭比娃娃.com」
    做几个经营「人」的小网站,速速卖给大公司?
    关于工作与生活来自前hp总裁孙振耀
    新网站上线,酷狗狗 www.coogogo.com
    中国地摊联盟群组
    discuz!nt论坛搬迁后出错,提示:对象名 'dnt_templates' 无效
    时光.旅人
    const和readonly
    html.partial的一个bug?
  • 原文地址:https://www.cnblogs.com/tmf-4838/p/5267500.html
Copyright © 2020-2023  润新知