UIPickerView普通选择器

xiaoxiao2025-11-01  20

#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

 

转载请注明原文地址: https://www.6miu.com/read-5038899.html

最新回复(0)