Objective
Uipickerview is a selector control that generates a Single-column selector or a multiple-column selector, and the developer can fully customize the appearance of the selection, so the usage is flexible. Uipickerview directly inherits UIView and does not inherit Uicontrol, so it cannot bind the event-handling method as Uicontrol, and Uipickerview event handling is done by its delegate object.
In this paper, the use of Uipickerview to achieve the city selector, the first listed provinces, the second listed as the first column of the corresponding city or region, the data in the plist, plist structure as shown in the following figure, the first layer is a dictionary, each province corresponds to the city is an array:
Implementation steps
First step
Drag into a UIPickerView
to StoryBoard
, and then set UIPickerViewDelegate
, and UIPickerViewDataSource
for the current controller, as shown in the following figure red area:
Setting up data sources and proxies
Second Step
Implemented in the corresponding ViewController
, code comments are very detailed
#import "ViewController.h" @interface Viewcontroller () <uipickerviewdelegate, uipickerviewdatasource>/** * PLI
St Corresponding Dictionary * * * @property (nonatomic, strong) nsdictionary* Citynames;
/** * Province * * * @property (nonatomic, Strong) nsarray* provinces;
/** * CITY * * * * @property (nonatomic, strong) nsarray* cities; @end @implementation viewcontroller/** * Lazy Load plist * * * @return plist corresponding Dictionary/-(nsdictionary*) Citynames {if (_cit
Ynames = = nil) {nsstring* path = [[NSBundle mainbundle] pathforresource:@ "Citydata" oftype:@ "plist"];
_citynames = [Nsdictionary Dictionarywithcontentsoffile:path];
return _citynames; /** * Lazy Loading provinces * * * @return provinces corresponding to the array/* (Nsarray *) Provinces {if (_provinces = = nil) {//Save province to array _provinc
ES = [Self.citynames AllKeys];
return _provinces;
}-(void) viewdidload {[Super viewdidload];
Do no additional setup after loading the view, typically from a nib. }-(void) didreceivememorywarning {[Super DIDRECEIVememorywarning];
Dispose of any of the can is recreated. /** * Returns the number of rows per column * * @param pickerview * @param component * * * @return * * (Nsinteger) Pickerview: (uipickerview*) pi
Ckerview numberofrowsincomponent: (Nsinteger) component {if (component = 0) {return self.provinces.count;
else {[Self loaddata:pickerview];
return self.cities.count; }/** * Returns the text displayed on each line * * @param pickerview * @param row * @param component * * @return * * (nsstring*) Pickerview :(uipickerview*) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component {//The first column returns all provinces if (component
= = 0) {return self.provinces[row];
else {[Self loaddata:pickerview];
return Self.cities[row]; }/** * Load the data displayed in the second column * * @param pickerview/-(void) LoadData: (uipickerview*) Pickerview {//Be sure to first get the user-selected line before you can
Acquire the province according to the selected line and then go to the dictionary to load the provinces corresponding to the city nsinteger selrow = [Pickerview selectedrowincomponent:0]; NSString *key = Self.provinces[selroW];
Self.cities = [Self.citynames Valueforkey:key]; How many/** * * * @param pickerview * * @return * * * (Nsinteger) Numberofcomponentsinpickerview: (uipickerview*) Picke
Rview {return 2;} /** * Key to callback linkage after selecting a row * * @param pickerview * @param the province of the row user selected * @param component/(void) Pickerview: (Uipicke rview*) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component {if (component = 0) {//Reload number of second column
According to [Pickerview Reloadcomponent:1];
Let the second column be returned [Pickerview selectrow:0 incomponent:1 Animated:yes]; }} @end
Implementation effect
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.