用UIPickerView来显示省和市

UIPickerView 是一个选择器控件, 它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活。UIPickerView 直接继承了 UIView ,没有继承 UIControl ,因此,它不能像 UIControl 那样绑定事件处理方法, UIPickerView 的事件处理由其委托对象完成。

正文: UIPickerView 控件常用的属性和方法如下:

  numberOfComponents: 获取UIPickerView指定列中包含的列表项的数量。该属性是一个只读属性。 

  showsSelectionIndicator: 该属性控制是否显示UIPickerView中的选中标记(以高亮背景作为选中标记)。 

  numberOfRowsInComponent: 获取UIPickerView包含的列数量。 

  rowSizeForComponent: 获取UIPickerView包含的指定列中列表项的大小。该方法返回一个CGSize对象。 

  selectRow:inComponent:animated:: 该方法设置选中该UIPickerView中指定列的特定列表项。最后一个参数控制是否使用动画。 

  selectedRowInComponent:: 该方法返回该UIPickerView指定列中被选中的列表项。 

  viewForRow:forComponent:: 该方法返回该UIPickerView指定列的列表项所使用的UIView控件。 

UIDatePicker控件只是负责该控件的通用行为,而该控件包含多少列,各列包含多少个列表项则由UIPickerViewDataSource对象负责。开发者必须为UIPickerView设置UIPickerViewDataSource对象,并实现如下两个方法。 

  numberOfComponentsInPickerView:: 该UIPickerView将通过该方法来判断应该包含多少列。 

  pickerView:numberOfRowsInComponent:: 该UIPickerView将通过该方法判断指定列应该包含多少个列表项。 

如果程序需要控制UIPickerView中各列的宽度,以及各列中列表项的大小和外观,或程序需要为UIPickerView的选中事件提供响应,都需要为UIPickerView设置UIPickerViewDelegate委托对象,并根据需要实现该委托对象中的如下方法。 

  pickerView:rowHeightForComponent:: 该方法返回的CGFloat值将作为该UIPickerView控件中指定列中列表项的高度。 

  pickerView:widthForComponent:: 该方法返回的CGFloat值将作为该UIPickerView控件中指定列的宽度。 

  pickerView:titleForRow:forComponent:: 该方法返回的NSString值将作为该UIPickerView控件中指定列的列表项的文本标题。 

  pickerView:viewForRow:forComponent:reusingView:: 该方法返回的UIView控件将直接作为该UIPickerView控件中指定列的指定列表项。 

  pickerView:didSelectRow:inComponent:: 当用户单击选中该UIPickerView控件的指定列的指定列表项时将会激发该方法 

Interface Builder只支持为UIPickerView设置一个属性——Shows Selection Indicator,该属性用于控制是否显示UIPickerView中的选中标记(以高亮背景作为选中标记)。

@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic ,strong) NSArray *arr;

@property (nonatomic ,strong) UIPickerView *picker;

@property (nonatomic ,strong) NSMutableArray *arr1;

@property (nonatomic ,strong) NSMutableArray *arr2;

@end

self.arr1 = [NSMutableArray array];

    self.arr2 = [NSMutableArray array];

    self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];

    self.picker.backgroundColor = [UIColor redColor];

    self.picker.dataSource =self;

    self.picker.delegate = self;

    [self.view addSubview:self.picker];

    

    NSString *path = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];

    self.arr = [NSArray arrayWithContentsOfFile:path];

    for(int i =0;i<self. arr.count;i++) {

       // NSDictionary *dic = self.arr[i];

        [self.arr1 addObject:self.arr[i][@"State"] ];

        

    }

  

   

    

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    

    return 2;

}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    if (component ==0)

    {

        return self.arr1.count;

    }

    else

    {

        

       return self.arr2.count;

    }

               

    }

    

  

 - (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

    if (component == 0)

    {

                return self.arr1[row];

    }

    

    else {

        

        return self.arr2[row];

        }

        

  

}

#pragma mark delegate 选中行的信息,

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

    if (component == 0) {

        [self.arr2 removeAllObjects];

        //[pickerView reloadComponent:1];

        NSInteger rowarr1 = [pickerView selectedRowInComponent:0];

        NSArray *arr1 =self.arr[rowarr1][@"Cities"];

       // NSArray *arr1 =self.arr[rowarr1][@"Cities"];

                for (int i =0; i< arr1.count; i++)

               {

                    [self.arr2  addObject:arr1[i][@"city"]];

                }

    

    

    [self.picker reloadComponent:1];

    }

    

    

    else

    {

    //刷新

    

        NSString *str = [NSString stringWithFormat:@"%@----%@",self.arr1[[pickerView selectedRowInComponent:0]],self.arr2[[pickerView selectedRowInComponent:1]]];

        

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示信息" message:str preferredStyle:UIAlertControllerStyleAlert];

        

        UIAlertAction  *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];

        UIAlertAction  *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];

        [alert addAction:action1];

        [alert addAction:action2];

        [self presentViewController:alert animated:YES completion:nil];

        

 }

}

原文地址:https://www.cnblogs.com/liumu/p/5267623.html