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

    }

  • 相关阅读:
    吴军博士:物联网和人工智能将再造一个英特尔和微软 | 万物互联
    速来膜拜!20位活跃在Github上的国内技术大牛
    创建带Mipmap的osg::Image
    C#文件系统管理【转】
    C#文本文件(.txt)读写 [转]
    C#连接SQL Server数据库进行简单操作[转]
    shell脚本把一些请求量非常高的ip给拒绝掉
    linux获取精准进程PID之pgrep命令
    Kubernetes的Cron Job
    StatefulSet和Deployment的区别
  • 原文地址:https://www.cnblogs.com/tmf-4838/p/5267500.html
Copyright © 2020-2023  润新知