Uipickerview is a selector control that can generate a single-column selector, or a multicolumn selector
@interface viewcontroller:uiviewcontroller<uipickerviewdatasource,uipickerviewdelegate>
@property (strong,nonatomic) Uipickerview *mypickerview;
@property (strong,nonatomic) Nsmutablearray *provincearr;
@property (strong,nonatomic) Nsmutablearray *cityarr;
@property (strong,nonatomic) Nsarray *arr;
@end
#import "ViewController.h"
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) viewdidload
{
[Super Viewdidload];
Self. Provincearr=[nsmutablearray array];
Self. Cityarr=[nsmutablearray array];
Path to the file
NSString *path=[[nsbundle Mainbundle] pathforresource:@ "City" oftype:@ "plist"];
Self.arr=[nsarray Arraywithcontentsoffile:path];
The traversal cycle of the province
for (int i=0;i<self.arr.count; i++)
{
[Self. Provincearr addobject:self.arr[i][@ "state"];
}
Picker control
Self. Mypickerview=[[uipickerview Alloc] Initwithframe:cgrectmake (70, 100, 300, 300)];
Self. mypickerview.delegate=self;
Self. mypickerview.datasource=self;
Self. Mypickerview.backgroundcolor=[uicolor Clearcolor];
[Self.view addsubview:self. Mypickerview];
}
#pragma mark data source Numberofcomponentsinpickerview
-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview
{
return 2;
}
#pragma mark data source Numberofrowsincomponent
-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component
{
if (component==0)
{
return self. Provincearr.count;
}
Else
{
return self. Cityarr.count;
}
}
#pragma delegate Display information methods
-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component
{
if (component==0)
{
return self. Provincearr[row];
}
return self. Cityarr[row];
}
#pragma mark Select BOC Information
-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component
{
if (component==0)
{
[Self. Cityarr Removeallobjects];
Nsinteger Rowprovincearr=[pickerview selectedrowincomponent:0];
Nsarray *arr11=self.arr[rowprovincearr][@ "Cities"];
The traversal cycle in the city
for (int i=0; i<arr11.count; i++)
{
[Self. Cityarr addobject:arr11[i][@ "City"];
}
Refresh Reload
[Self. Mypickerview Reloadcomponent:1];
}
Else
{
Uialertview *myalertview=[[uialertview alloc] initwithtitle:@ "System tip" message:[nsstring stringwithformat:@ "%@--%@ ", [_provincearr objectatindex:row],[_cityarr objectatindex:row]]delegate:nil cancelbuttontitle:@" Cancel " otherbuttontitles:@ "OK", nil];
[Myalertview show];
NSString *str=[nsstring stringwithformat:@ "%@--%@", _provincearr[[pickerview selectedrowincomponent:0]],_cityarr[ Row]];
Prompt box
Uialertcontroller *myalertcontroller=[uialertcontroller alertcontrollerwithtitle:@ "System Tips" message:str Preferredstyle:uialertcontrollerstylealert];
Uialertaction *myalertaction=[uialertaction actionwithtitle:@ "OK" Style:uialertactionstyledefault handler:^ ( Uialertaction * _nonnull Action)
{
NSLog (@ "OK");
}];
Uialertaction *myalertaction1=[uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstyledefault handler:^ ( Uialertaction * _nonnull Action)
{
NSLog (@ "Cancel");
}];
[Myalertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield * _nonnull TextField)
{
Textfield.backgroundcolor=[uicolor Whitecolor];
}];
[Myalertcontroller Addaction:myalertaction1];
[Myalertcontroller addaction:myalertaction];
[Self Presentviewcontroller:myalertcontroller animated:yes completion:nil];
}
}
#pragma mark shows the row height
-(CGFloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (Nsinteger) component
{
return 50.00;
}
iOS development--uipickerview (selector control) practices for provinces and cities