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 selected item, so it's very flexible to use. 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.
Body: The properties and methods commonly used by Uipickerview controls are as follows:
Numberofcomponents: Gets the number of list items contained in the Uipickerview specified column. This property is a read-only property.
Showsselectionindicator: This property controls whether the check mark in the Uipickerview is displayed (with a highlighted background as a check mark).
Numberofrowsincomponent: Gets the number of columns that Uipickerview contains.
Rowsizeforcomponent: Gets the size of the list item in the specified column Uipickerview contains. The method returns a Cgsize object.
SelectRow:inComponent:animated:: This method setting selects a specific list item for the specified column in the Uipickerview. The last parameter controls whether animations are used.
Selectedrowincomponent:: This method returns the list item that is selected in the Uipickerview specified column.
Viewforrow:forcomponent:: This method returns the UIView control used by the list item for the Uipickerview specified column.
The Uidatepicker control is responsible only for the common behavior of the control, how many columns the control contains, and how many list items the columns contain, which are owned by the Uipickerviewdatasource object. The developer must set the Uipickerviewdatasource object for Uipickerview and implement the following two methods.
Numberofcomponentsinpickerview:: The Uipickerview will use this method to determine how many columns should be included.
Pickerview:numberofrowsincomponent:: The Uipickerview will determine by this method how many list items the specified column should contain.
If your program needs to control the width of the columns in the Uipickerview, as well as the size and appearance of the list items in each column, or the program needs to provide a response for the Uipickerview selected event, You need to set the Uipickerviewdelegate delegate object for Uipickerview and implement the following methods in the delegate object as needed.
Pickerview:rowheightforcomponent:: The CGFloat value returned by this method will be the height of the list item in the specified column in the Uipickerview control.
Pickerview:widthforcomponent:: The CGFloat value returned by this method will be the width of the specified column in the Uipickerview control.
PickerView:titleForRow:forComponent:: The NSString value returned by this method will serve as the text caption for the list item of the specified column in the Uipickerview control.
PickerView:viewForRow:forComponent:reusingView:: The UIView control returned by this method will be used directly as the specified list item for the specified column in the Uipickerview control.
PickerView:didSelectRow:inComponent:: This method fires when the user clicks the specified list item for the specified column of the Uipickerview control to be selected
Interface Builder only supports setting a property for Uipickerview--shows Selection indicator, which controls whether the check mark in the Uipickerview is displayed (with a highlighted background as a check mark).
Example: through the prior knowledge popularization, we know the use of Uipickerview, the following a small example, to a more comprehensive understanding.
1. Drag a control over the Controllerview in storyboard and link to the. h file with the following code:
@property (Weak, nonatomic) Iboutlet Uipickerview *picker;
2. Set up the proxy in the. m file and set the global variable with the following code:
#import "ViewController.h"
@interface Viewcontroller () <UIPickerViewDelegate,UIPickerViewDataSource>
{
Nsarray *_province;
Nsdictionary *_city;
Nsdictionary *_country;
}
@end
3. Set up the city province data, the code is as follows:
Province
_province = @[@ "Beijing", @ "Guangxi", @ "Guangdong"];
City
_city = @{
@ "Beijing": @[@ "Chaoyang District", @ "Dongcheng District", @ "Xicheng"],
@ "Guangxi": @[@ "Guilin", @ "Nanning"],
@ "Guangdong": @[@ "Huizhou", @ "Guangzhou", @ "Shenzhen", @ "Dongguan"]
County District
_country = @{
@ "Chaoyang District": @[@ "Chaoyang District 1", @ "Chaoyang District 2", @ "Chaoyang District 3"],
@ "Dongcheng District": @[@ "Dongcheng District 1", @ "Dongcheng District 2", @ "Dongcheng District 3", @ "Dongcheng District 4"],
@ "Xicheng": @[@ "Xicheng 1", @ "Xicheng 2", @ "Xicheng 3", @ "Xicheng 4"],
@ "Guilin": @[@ "Guilin 1", @ "Guilin 2", @ "Guilin 3"],
@ "Nanning": @[@ "Nanning 1", @ "Nanning 2", @ "Nanning 3", @ "Nanning 4"],
@ "Huizhou": @[@ "Huizhou 1", @ "Huizhou 2", @ "Huizhou 3", @ "Huizhou 4"],
@ "Guangzhou": @[@ "Guangzhou City 1", @ "Guangzhou City 2", @ "Guangzhou 3"],
@ "Shenzhen": @[@ "Shenzhen City 1", @ "Shenzhen 2", @ "Shenzhen 3", @ "Shenzhen 4"],
@ "Dongguan": @[@ "Dongguan 1", @ "Dongguan 2", @ "Dongguan 3", @ "Dongguan 4"],
};
4. Binding agent, the code is as follows:
Self.picker.dataSource = self;
Self.picker.delegate = self;
5. Set the Uipickerview data source method with the following code:
#pragma mark-The return value of the method determines how many columns the control contains
-(Nsinteger) Numberofcomponentsinpickerview: (uipickerview*) Pickerview
{
return 3;
}
#pragma mark-The return value of this method determines how many list items the control specifies the column contains
-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component{
if (0 = component)
{
return _province.count;
}
if (1 = component) {
Nsinteger rowprovince = [Pickerview selectedrowincomponent:0];
NSString *provincename = _province[rowprovince];
Nsarray *citys = _city[provincename];
return citys.count;
}else{
Nsinteger rowprovince = [Pickerview selectedrowincomponent:0];
NSString *provincename = _province[rowprovince];
Nsarray *citys = _city[provincename];
Nsinteger rowcity = [Pickerview selectedrowincomponent:1];
NSString *cityname = citys[rowcity];
Nsarray *country = _country[cityname];
return country.count;
}
}
6. Set the Uipickerview proxy method, the code is as follows:
#pragma mark-The NSString returned by this method will be the caption text for the specified column and list item in Uipickerview
-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component
{
if (0 = component) {
return _province[row];
}
if (1 = component) {
Nsinteger rowprovince = [Pickerview selectedrowincomponent:0];
NSString *provincename = _province[rowprovince];
Nsarray *citys = _city[provincename];
return Citys[row];
}else{
Nsinteger rowprovince = [Pickerview selectedrowincomponent:0];
NSString *provincename = _province[rowprovince];
Nsarray *citys = _city[provincename];
Nsinteger rowcity = [Pickerview selectedrowincomponent:1];
NSString *cityname = citys[rowcity];
Nsarray *country = _country[cityname];
return Country[row];
}
}
7. When a row is selected, the following code can be used to obtain the chosen urban area.
#pragma mark-fires the method when the user selects the specified column and list item in Uipickerviewdatasource
-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component
{
if (0 = component) {
[Pickerview reloadcomponent:1];
[Pickerview Reloadcomponent:2];
}
if (1 = component)
[Pickerview Reloadcomponent:2];
Nsinteger Rowone = [Pickerview selectedrowincomponent:0];
Nsinteger Rowtow = [Pickerview selectedrowincomponent:1];
Nsinteger rowthree = [Pickerview selectedrowincomponent:2];
NSString *provincename = _province[rowone];
Nsarray *citys = _city[provincename];
NSString *cityname = Citys[rowtow];
Nsarray *countrys = _country[cityname];
NSLog (@ "%@~%@~%@", _province[rowone], Citys[rowtow],countrys[rowthree]);
}
Run result: Well, through the above steps, you can create a simple city selector. The operation effect is as follows:
Note: The blue button "OK" that line, is a Uitoolbar control, this will not be detailed, otherwise it will be a little distracting, interested friends, you can communicate in private, or let's.