UIPickerView普通选择器

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (strong,nonatomic) UIPickerView *pickerView;
@property (strong,nonatomic) UILabel *label;
@property (strong,nonatomic) UIButton *button;
@property (strong,nonatomic) NSDictionary *pickerData;
@property (strong,nonatomic) NSArray *pickerDataProvinces;
@property (strong,nonatomic) NSArray *pickerDataCities;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取列表文件所有数据
    NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"provinces_cities" ofType:@"plist"];
    NSDictionary *dict =[[NSDictionary alloc]initWithContentsOfFile:plistPath];
    self.pickerData = dict;
    //省分名数据
    self.pickerDataProvinces = [self.pickerData allKeys];
    //第一个省所有市的数据
    NSString *selectedProvince = [self.pickerDataProvinces objectAtIndex:0];
    self.pickerDataCities = [self.pickerData objectForKey:selectedProvince];
    
    CGRect screen = [[UIScreen mainScreen]bounds];
    
    //pickerView
    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(screen.size.width/2-160, 0, 320, 162)];
    [self.view addSubview:self.pickerView];
    //label
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(screen.size.width/2-100, 273, 200, 21)];
    self.label.text = @"label";
    self.label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.label];
    //button
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.button setTitle:@"button" forState:UIControlStateNormal];
    self.button.frame = CGRectMake(screen.size.width/2-23, 374, 46, 30);
    [self.button addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    //分配协议给实现对象
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
}

-(void)onclick:(id)sender{
    NSInteger row1 = [self.pickerView selectedRowInComponent:0];
    NSInteger row2 = [self.pickerView selectedRowInComponent:1];
    NSString* selected1 = [self.pickerDataProvinces objectAtIndex:row1];
    NSString* selected2 = [self.pickerDataCities objectAtIndex:row2];
    NSString* title = [[NSString alloc]initWithFormat:@"%@,%@",selected1,selected2];
    self.label.text = title;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --实现协议UIPickerViewDataSource方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {//省份个数
        return [self.pickerDataProvinces count];
    } else {//市的个数
        return [self.pickerDataCities count];
    }
    
}

#pragma mark --实现协议UIPickerViewDelegate方法
-(NSString *)pickerView:(UIPickerView *)pickerView
            titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {//选择省份名
        return [self.pickerDataProvinces objectAtIndex:row];
    } else {//选择市名
        return [self.pickerDataCities objectAtIndex:row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        NSString *seletedProvince = [self.pickerDataProvinces objectAtIndex:row];
        NSArray *array = [self.pickerData objectForKey:seletedProvince];
        self.pickerDataCities = array;
        [self.pickerView reloadComponent:1];
    }
}



@end

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值