EYCNCityPicker 0.0.3

EYCNCityPicker 0.0.3

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Jun 2017

Maintained by Enix Yu.



  • By
  • Enix Yu

iOS城市选择器,行政区域来源于高德web API。

screenshot

安装依赖

使用方法

  1. 获取行政区域数据
EYCNCityService *service = [[EYCNCityService alloc] initWithAPIKey:@"699161121d34385622a16ca815c6a934"];
    [service requestCNCitiesWithCompletion:^(NSArray * _Nullable cities, NSError * _Nullable error) {
        if (error) {
            NSLog(@"获取行政区域失败,原因: %@", error.localizedDescription);
        } else {
            self.data = cities;
        }
    }];
  1. 显示picker
    /// picker中元素的样式
    NSDictionary *attr = @{NSForegroundColorAttributeName: [UIColor darkGrayColor],
                           NSFontAttributeName: [UIFont systemFontOfSize:16]};
    
    /// 初始选择
    NSArray *selections = @[@(_selectProvinceIdx), @(_selectCityIdx), @(_selectDistrictIdx)];
    
    /// 构建一个picker
    EYCNCityPicker *picker = [EYCNCityPicker buildPicker];
    
    /// 显示picker
    [picker
     showInView:self.view
     withData:self.data
     initialSelctions:selections
     itemAttributes:attr
     cancel:^(EYCNCityPicker *picker) {
         // 用户取消选择
         NSLog(@"User canceled");
         [self.selectButton setTitle:@"未选择" forState:UIControlStateNormal];
     } confirmBlock:^(EYCNCityPicker * _Nonnull picker, NSInteger selectedProvinceIdx, NSInteger selectedCityIdx, NSInteger selectedDistrictIdx, NSString * _Nullable selectProvince, NSString * _Nullable selectCity, NSString * _Nullable selectDistrict) {
         // 用户选择了某个行政区域
         self.selectProvinceValue = selectProvince;
         self.selectCityValue = selectCity == nil ? @"" : selectCity;
         self.selectDistrictValue = selectDistrict == nil ? @"" : selectDistrict;
         
         self.selectProvinceIdx = selectedProvinceIdx;
         self.selectCityIdx = selectedCityIdx;
         self.selectDistrictIdx = selectedDistrictIdx;
         
         NSString *selection = [NSString stringWithFormat:@"%@, %@, %@", _selectProvinceValue, _selectCityValue, _selectDistrictValue];
         [self.selectButton setTitle:selection forState:UIControlStateNormal];
     }];

更多使用例子,请查看EYCNCityPickerExample