Latest 0.0.3
Homepage https://github.com/enix223/EYCNCityPicker
License MIT
Platforms ios 8.0, requires ARC
Authors

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

screenshot

安装依赖

Cocoapods

Podfile 文件中增加如下行

pod 'EYCNCityPicker'

运行:

pod install

使用方法

  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

Latest podspec

{
    "name": "EYCNCityPicker",
    "version": "0.0.3",
    "summary": "iOSu4e2du56fdu884cu653fu533au57dfpickeru63a7u4ef6uff0cu6570u636eu6765u6e90u4e8eu9ad8u5fb7web API",
    "homepage": "https://github.com/enix223/EYCNCityPicker",
    "license": "MIT",
    "authors": {
        "Enix Yu": "[email protected]"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/enix223/EYCNCityPicker.git",
        "tag": "0.0.3"
    },
    "source_files": [
        "Sources",
        "Sources/**/*.{h,m}"
    ],
    "resources": "Sources/**/*.{xib}",
    "exclude_files": "Sources/Exclude",
    "requires_arc": true
}

Pin It on Pinterest

Share This