This demand, is the provincial three-level linkage selection, and the city's choice to be based on the province's ID, and then the selection of the district to be based on the city's ID
Every time the network request, the key code, is actually
Monitor the movement of the wheel
-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component;
Then I upload my code, this code I also based on other people's changes, because the demand is not the same
1, create a view
//
IWAreaPickerView.h
Internetbank
//
Created by Zmit on 2017/9/8.
Copyright? 2017 Creditcloud. All rights reserved.
//
#import <UIKit/UIKit.h>
Custom Proxy Protocol
@protocol iwareapickerviewdelegate <NSObject>
-(void) addresspickerviewdidselected: (NSString *) areaname withdic: (nsdictionary *) info;//Click on the Finish button above the proxy return to the stitching good selected address
-(void) addresspickerviewdidclose;//Click Close Agent
@end
@interface Iwareapickerview:uiview
@property (Weak, nonatomic) id<iwareapickerviewdelegate> delegate;
@end
//
iwareapickerview.m
Internetbank
//
Created by Zmit on 2017/9/8.
Copyright? 2017 Creditcloud. All rights reserved.
//
#import "IWAreaPickerView.h"
JSON type #import "ProvincesModel.h"//Province
#import JSON type for "CitysModel.h"//City
JSON type for #import "AreasModel.h"//Area
#import "APIClient.h"
static const Nsinteger kprovincecomponent = 0;
static const Nsinteger kcitycomponent = 1;
static const Nsinteger kdistrictcomponent = 2;
#define KFONT14 [Uifont systemfontofsize:14]
@interface Iwareapickerview () <uipickerviewdatasource, uipickerviewdelegate>
@property (Strong, nonatomic) Uipickerview *pickerview;
@property (Strong, nonatomic) Provincemodel *provbridge;
@property (Strong, nonatomic) Citymodel *citybridge;
@property (Strong, nonatomic) Areamodel *areabridge;
@property (Strong, nonatomic) Nsmutablearray *provdataarr;//province Array
@property (Strong, nonatomic) Nsmutablearray *citydataarr;//City Array
@property (Strong, Nonatomic) Nsmutablearray * areadataarr;//Area array
@end
@implementation Iwareapickerview
{
Nsinteger _selectrow0;//record selection line for NO. 0 wheel
Nsinteger _selectrow1;
Nsinteger _selectrow2;
NSString *_areastring;//The last detail address stitching string to be passed back
Nsdictionary *_infodic;
Provincemodel *_promodel;//Record the data of the selected province
Citymodel *_citymodel;
Areamodel *_areamodel;
}
-(Instancetype) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if (self) {
[Self setup];
Self.provdataarr = [[Nsmutablearray alloc]init];
Self.citydataarr = [[Nsmutablearray alloc]init];
Self.areadataarr = [[Nsmutablearray alloc]init];
}
return self;
}
-(void) Setup {
_selectrow0 = 0;
_selectrow1 = 0;
_selectrow2 = 0;
Self.backgroundcolor = [Uicolor colorwithred:222/255.0 green:227/255.0 blue:237/255.0 alpha:1.0];
Uitoolbar *toolbar = [[Uitoolbar alloc] Initwithframe:cgrectmake (0, 0, cgrectgetwidth (self.bounds), 44)];
Toolbar.backgroundcolor = [Uicolor Clearcolor];
[Self addsubview:toolbar];
Uibarbuttonitem *closeitem = [[Uibarbuttonitem alloc]initwithimage:[uiimage imagenamed:@ ' icon_x '] style: Uibarbuttonitemstyleplain target:self Action: @selector (Selectaddressclose)];
Uibarbuttonitem *spaceitem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem: Uibarbuttonsystemitemflexiblespace Target:nil Action:nil];
Uibarbuttonitem *titleitem = [[Uibarbuttonitem alloc] initwithtitle:@ "Select area" Style:uibarbuttonitemstyleplain target: Self action: @selector (Selectaddressclose)];
Uibarbuttonitem *spaceitem2 = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem: Uibarbuttonsystemitemflexiblespace Target:nil Action:nil];
Uibarbuttonitem *completeitem = [[Uibarbuttonitem alloc]initwithimage:[uiimage imagenamed:@ ' icon_right '] style: Uibarbuttonitemstyleplain target:self Action: @selector (Selectaddresscomplete)];
Toolbar.items = @[closeitem, spaceitem,titleitem,spaceitem2, Completeitem];
Self.pickerView.frame = CGRectMake (0, 44, cgrectgetwidth (Self.bounds), Cgrectgetheight (self.bounds)-);
[Self addSubview:self.pickerView];
[Self Downloadprov];
}
#pragma mark-http methods
/* Province */
-(void) Downloadprov
{
NSString *url = [[Apiclient instance]apienjoyaddressofgetprovice:@ "IOS" withpromotion:@ "0000"];
[Self.provdataarr removeallobjects];
[[Apiclient Instance]getgoodsprovince:url success:^ (Provincesmodel *result)
{
if (result)
{
[Self.provdataarr AddObjectsFromArray:result.data];
[Self pickerView:self.pickerView didselectrow:0 incomponent:0];/ /linkage wheel 1 must have data in this round to trigger Didselect
[Self.pickerview reloadallcomponents];
}
} failure:^ (NSString *error) {
}];
}
/* City * *
-(void) Downloadcitywithid: (NSString *) provid
{
NSString *url = [[Apiclient instance]apienjoyaddressofgetcitiesbyprovincecode:provid withDeviceType:@ "IOS" withpromotion:@ "0000"];
[Self.citydataarr removeallobjects];
[[Apiclient Instance]getgoodscity:url success:^ (Citysmodel *result)
{
if (result)
{
[Self.citydataarr AddObjectsFromArray:result.data];
[Self.pickerview reloadcomponent:1];
[Self.pickerview selectrow:0 incomponent:1 animated:yes];//Default selection row0
[Self pickerView:self.pickerView didselectrow:0 incomponent:1];//linkage wheel 2 must have data in this round to trigger Didselect
_citymodel = Self.citydataarr[_selectrow1];
[Self downloadareawithid:_citymodel.citycode];
}
} failure:^ (NSString *error) {
}];
}
/* Area */
-(void) Downloadareawithid: (NSString *) Cityid
{
NSString *url = [[apiclient instance]apienjoyaddressofgetareabycitycode:cityid withdevicetype:@ ' IOS ' withPromotion:@ "0000"];
[Self.areadataarr removeallobjects];
[[Apiclient Instance]getgoodsarea:url success:^ (Areasmodel *result)
{
if (result)
{
[Self.areadataarr AddObjectsFromArray:result.data];
[Self.pickerview Reloadcomponent:2];
[Self.pickerview selectrow:0 incomponent:2 Animated:yes];
[Self PickerView:self.pickerView didselectrow:0 incomponent:2];
}
} failure:^ (NSString *error) {
}];
}
#pragma mark-events response
-(void) Selectaddresscomplete {
[Self.delegate addresspickerviewdidselected:_areastring Withdic:_infodic];
}
-(void) Selectaddressclose {
[Self.delegate Addresspickerviewdidclose];
}
#pragma mark-uipickerviewdatasource
Determine the number of wheels in the picker
-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview {
return 3;
}
Determine the item number for each wheel of the picker
-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) Component {
if (component==0) {
return self.provDataArr.count;
}else if (component==1) {
return self.cityDataArr.count;
}else{
return self.areaDataArr.count;
}
}
-(CGFloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (Nsinteger) component{
return 40;
}
Determine what is displayed for each item on each wheel
-(Nsattributedstring *) Pickerview: (Uipickerview *) Pickerview Attributedtitleforrow: (nsinteger) row forComponent: ( Nsinteger) component{
Nsdictionary * Attrdic = @{nsforegroundcolorattributename:[uicolor Blackcolor],nsfontattributename:[uifont Systemfontofsize:6]};
Provincemodel *provice = [[Provincemodel alloc]init];
Citymodel *city = [[Citymodel alloc]init];
Areamodel *area = [[Areamodel alloc]init];
Nsattributedstring * attrstring;
if (component==0) {
Provice = (Provincemodel *) Self.provdataarr[row];
attrstring = [[Nsattributedstring alloc] InitWithString:provice.provinceName attributes:attrdic];
}else if (component==1) {
City = (Citymodel *) Self.citydataarr[row];
attrstring = [[Nsattributedstring alloc] InitWithString:city.cityName attributes:attrdic];
}else{
Area = (Areamodel *) Self.areadataarr[row];
attrstring = [[Nsattributedstring alloc] InitWithString:area.areaName attributes:attrdic];
}
return attrstring;
}
Monitor the movement of the wheel
-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) Component {
if (component==0) {
_selectrow0 = [Pickerview selectedrowincomponent:0];
_selectrow1 = 0;
_selectrow2 = 0;
_promodel = Self.provdataarr[_selectrow0];
[Self downloadcitywithid:_promodel.provincecode];
}else if (component==1) {
_selectrow1 = [Pickerview selectedrowincomponent:1];
_selectrow2 = 0;
_citymodel = Self.citydataarr[_selectrow1];
[Self downloadareawithid:_citymodel.citycode];
}else{
_selectrow2 = [Pickerview selectedrowincomponent:2];
if (self.areadataarr&&self.areadataarr.count>0) {
_areamodel = Self.areadataarr[_selectrow2];
}else{
_areamodel = nil;
}
}
if (_areamodel==nil) {
_areastring = [NSString stringwithformat:@ "%@%@", _promodel.provincename,_citymodel.cityname];
if (_promodel && _citymodel)
{
_infodic = @{
@ "Proid": _promodel.provincecode,
@ "Cityid": _citymodel.citycode,
};
}
NSLog (@ "_info=%@", _infodic);
}else{
_areastring = [NSString stringwithformat:@ "%@%@%@", _promodel.provincename,_citymodel.cityname,_areamodel.areaname ];
if (_promodel && _citymodel && _areamodel)
{
_infodic = @{
@ "Proid": _promodel.provincecode,
@ "Cityid": _citymodel.citycode,
@ "Areaid": _areamodel.areacode,
};
}
NSLog (@ "_info=%@", _infodic);
}
}
modifying font styles
-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component Reusingview: (UIView *) view
{
UILabel *myview = nil;
CGFloat myvieww = PickerView.frame.size.width *0.33;
CGFloat MYVIEWH = 60;
MyView = [[UILabel alloc] initwithframe:cgrectmake (0.0, 0.0, MYVIEWW, MYVIEWH)];
Myview.font = KFont14;
Myview.backgroundcolor = [Uicolor Clearcolor];
Myview.textalignment = Nstextalignmentcenter;
if (component = = Kprovincecomponent) {
Self.provbridge = [_provdataarr objectatindex:row];
Myview.text = Self.provBridge.provinceName;
}
else if (component = = Kcitycomponent) {
Self.citybridge = [_citydataarr objectatindex:row];
Myview.text = Self.cityBridge.cityName;
}
else {
Self.areabridge = [_areadataarr objectatindex:row];
Myview.text = Self.areaBridge.areaName;
}
return myView;
}
#pragma mark-getters and setters
-(Uipickerview *) Pickerview {
if (_pickerview = = nil) {
_pickerview = [[Uipickerview alloc] init];
_pickerview.delegate = self;
_pickerview.datasource = self;
}
return _pickerview;
}
@end
The implementation of VC
{NSLog (@ "select Address");
[[UIApplication Sharedapplication].keywindow AddSubview:self.maskView];
_areapickerview = [[Iwareapickerview alloc] Initwithframe:cgrectmake (0, kDeviceHeight-350, Self.view.frame.size.width, 350)];
_areapickerview.delegate = self;
[Self.maskview Addsubview:_areapickerview];
}
Implementation of/*********************** Agent ***********************/
#pragma mark-iwareapickerviewdelegate
-(void) addresspickerviewdidselected: (NSString *) areaname withdic: (nsdictionary *) info{
AddressInfo = [Nsdictionary dictionarywithdictionary:info];
NSLog (@ "areaname =%@", areaname);
Addaddressview.nflag = @ "has";
Addaddressview.addressinfo = AreaName;
[Self addresspickerviewdidclose];
}
iOS delivery address level three linkage selection