OC 實現地址選擇器
阿新 • • 發佈:2018-12-19
1.在專案根目錄下儲存存有省市區相關資料的address.json檔案; 2.建立繼承自彈出檢視UpView的類AddressPickView:
#import "UpView.h" @protocol AddressPickViewDelegate <NSObject> -(void)didClickSureWithProvince:(NSString *)province City:(NSString *)city Area:(NSString *)area; @end @interface AddressPickView : UpView @property (nonatomic, strong) UIButton *sureBtn; @property (nonatomic, strong) UIButton *cancelBtn; @property (nonatomic, strong) UIPickerView *pickerView; @property (nonatomic, strong) NSArray *dataSource; @property (nonatomic, weak)id<AddressPickViewDelegate>delegate; @end
#import "AddressPickView.h" @interface AddressPickView ()<UIPickerViewDelegate,UIPickerViewDataSource> @property (nonatomic, strong)NSArray *provinces;//所有的省份陣列 @property (nonatomic, strong)NSString *selectedProvince;//當前選中的省份 @property (nonatomic, strong)NSString *selectedCity;//當前選中的城市 @property (nonatomic, strong)NSString *selectedArea;//當前選中的地區 @end @implementation AddressPickView /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ - (instancetype)init { if ([super init]) { self.dataSource = [self getDataSourceFromJson]; self.provinces = [self getProvincesFromDataSource]; self.selectedProvince = @"北京市"; self.selectedCity = @"北京"; self.selectedArea = @"東城區"; } return self; } - (void)setContentView { //建立按鈕 [self.contentView addSubview:self.sureBtn]; [self.contentView addSubview:self.cancelBtn]; //建立選擇檢視 [self.contentView addSubview:self.pickerView]; } #pragma mark - 選擇檢視 - (UIPickerView *)pickerView { if (!_pickerView) { _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, UI_SCREEN_WIDTH, self.contentHeight - 40)]; _pickerView.dataSource = self; _pickerView.delegate = self; } return _pickerView; } #pragma mark - UIPickerViewDelegate UIPickerViewDataSource - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 3; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component == 0) { return self.provinces.count; }else if (component == 1){ return [self getCitysFromProvince:self.selectedProvince].count; }else if (component == 2){ return [self getAreasFromProvince:self.selectedProvince AndCity:self.selectedCity].count; } return 0; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component == 0) { return self.provinces[row]; }else if (component == 1){ return [[self getCitysFromProvince:self.selectedProvince] objectAtIndex:row]; }else if (component == 2){ return [[self getAreasFromProvince:self.selectedProvince AndCity:self.selectedCity]objectAtIndex:row]; } return @""; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component == 0) { self.selectedProvince = [self.provinces objectAtIndex:row]; self.selectedCity = [self getCitysFromProvince:self.selectedProvince].firstObject; self.selectedArea = [self getAreasFromProvince:self.selectedProvince AndCity:self.selectedCity].firstObject; [pickerView reloadComponent:1]; [pickerView reloadComponent:2]; }else if (component == 1){ self.selectedCity = [[self getCitysFromProvince:self.selectedProvince] objectAtIndex:row]; self.selectedArea = [self getAreasFromProvince:self.selectedProvince AndCity:self.selectedCity].firstObject; [pickerView reloadComponent:2]; }else if (component == 2){ self.selectedArea = [[self getAreasFromProvince:self.selectedProvince AndCity:self.selectedCity] objectAtIndex:row]; } } #pragma mark - 從地址檔案中讀取所有的資料 - (NSArray *)getDataSourceFromJson{ NSString *path = [[NSBundle mainBundle]pathForResource:@"address" ofType:@"json"]; NSData *data = [[NSData alloc]initWithContentsOfFile:path]; NSArray *dataSource = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; return dataSource; } #pragma mark - 從資料來源中讀取所有的省名陣列 - (NSArray *)getProvincesFromDataSource { NSMutableArray *provinces = [NSMutableArray array]; for (NSDictionary *dic in self.dataSource) { NSString *province = dic.allKeys.firstObject; [provinces addObject:province]; } return provinces; } #pragma mark - 根據省獲取所有的市陣列 - (NSArray *)getCitysFromProvince:(NSString *)province { NSMutableArray *citys = [NSMutableArray array]; NSInteger index = [self.provinces indexOfObject:province]; NSDictionary *dic = self.dataSource[index]; NSArray *cityDataSource = dic[province]; for (NSDictionary *dic in cityDataSource) { NSString *city = dic.allKeys.firstObject; [citys addObject:city]; } return citys; } #pragma mark - 根據市獲取所有的區陣列 - (NSArray *)getAreasFromProvince:(NSString*)province AndCity:(NSString *)city { NSInteger provinceIndex = [self.provinces indexOfObject:province]; NSDictionary *provinceDic = self.dataSource[provinceIndex]; NSArray *cityDataSource = provinceDic[province]; NSArray *citys = [self getCitysFromProvince:province]; NSInteger cityIndex = [citys indexOfObject:city]; NSLog(@"%@",cityDataSource); NSDictionary *cityDic = cityDataSource[cityIndex]; NSArray *areas = cityDic[city]; return areas; } #pragma mark - 確定按鈕 - (UIButton *)sureBtn { if (!_sureBtn) { _sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(UI_SCREEN_WIDTH - 60, 0, 60, 40)]; [_sureBtn setTitle:@"確定" forState:UIControlStateNormal]; [_sureBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [_sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside]; } return _sureBtn; } #pragma mark - 取消按鈕 - (UIButton *)cancelBtn { if (!_cancelBtn) { _cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 40)]; [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal]; [_cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [_cancelBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside]; } return _cancelBtn; } #pragma mark -確定選擇 - (void)sureAction { if ([self.delegate respondsToSelector:@selector(didClickSureWithProvince:City:Area:)]) { [self.delegate didClickSureWithProvince:self.selectedProvince City:self.selectedCity Area:self.selectedArea]; } [self dismiss]; } #pragma mark -重寫彈出 - (void)show { if (!self.contentHeight) { self.contentHeight = 210.f; } [self setContentView]; [[self lastWidow] addSubview:self]; [UIView animateWithDuration:0.3 animations:^{ [self.contentView setFrame:CGRectMake(0, UI_SCREEN_HEIGHT - HOME_INDICATOR_HEIGHT - self.contentHeight, UI_SCREEN_WIDTH, self.contentHeight)]; }completion:^(BOOL finished) { }]; } #pragma mark - 獲取最上層window - (UIWindow *)lastWidow{ NSArray *windows = [UIApplication sharedApplication].windows; for (UIWindow *window in [windows reverseObjectEnumerator]) { if ([window isKindOfClass:[UIWindow class]] && CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds)) { return window; } } return [UIApplication sharedApplication].keyWindow; } @end
3.使用方法:
AddressPickView *addressV = [[AddressPickView alloc]init];
addressV.contentHeight = 300.0f;
addressV.delegate = self;
[addressV show];