Create a class that implements the basic functionality of Uipickerview
#import "PickerViewController.h"
Compliance with Uipickerviewdatasource,uipickerviewdelegate Protocol
@interface Pickerviewcontroller () <UIPickerViewDataSource,UIPickerViewDelegate>
Declaring a simple selector
@property (Nonatomic,strong) Uipickerview *picker;
Picker array of data sources for the first column of data
@property (Nonatomic,strong) Nsmutablearray *firstlist;
Picker data source array for the second column of data
@property (Nonatomic,strong) Nsmutablearray *secondlist;
Used to display selected content on a simple selector
@property (Nonatomic,strong) UILabel *showlable;
Picker Select the Click button to update the data on the lable
@property (Nonatomic,strong) UIButton *button;
Picker the data selected in the first column
@property (nonatomic,copy) NSString *firststr;
Picker the data selected in the second column
@property (nonatomic,copy) NSString *secondstr;
@end
@implementation Pickerviewcontroller
Picker lazy loading for picker initialization and setup
-(Uipickerview *) picker{
if (!_picker) {
Self.picker = [[Uipickerview alloc] Initwithframe:cgrectmake (40)];
_picker.shouldgroupaccessibilitychildren = YES;
_picker.delegate = self;
_picker.datasource = self;
}
return _picker;
}
-(UIButton *) button{
if (!_button) {
Self.button = [UIButton buttonwithtype:uibuttontypecustom];
_button.frame = CGRectMake (self.view.bounds.size.width-300, 30);
[_button settitle:@ "confirm" forstate:uicontrolstatenormal];
[_button addtarget:self Action: @selector (handlebuttonaction:) forcontrolevents:uicontroleventtouchupinside];
_button.backgroundcolor = [Uicolor Cyancolor];
}
return _button;
}
-(void) Handlebuttonaction: (UIButton *) sender{
Self.showLable.text = [NSString stringwithformat:@ "%@%@", Self.firststr,self.secondstr];
}
Display selection data for picker
-(UILabel *) showlable{
if (!_showlable) {
self.showlable = [[UILabel alloc] Initwithframe:cgrectmake (40)];
_showlable.backgroundcolor = [Uicolor Yellowcolor];
_showlable.textalignment = Nstextalignmentcenter;
_showlable.text = @ "1 1";
}
return _showlable;
}
Settings for the data source
-(Nsmutablearray *) firstlist{
if (!_firstlist) {
Self.firstlist = [Nsmutablearray arraywithobjects:@ "1", @ "2", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", @ "ten", nil];
}
return _firstlist;
}
-(Nsmutablearray *) secondlist{
if (!_secondlist) {
Self.secondlist = [Nsmutablearray arraywithobjects:@ "1", @ "2", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", @ "ten", nil];
}
return _secondlist;
}
-(void) Viewdidload {
[Super Viewdidload];
Do any additional setup after loading the view.
Adding controls on a view
[Self.view AddSubview:self.picker];
[Self.view AddSubview:self.button];
[Self.view addSubview:self.showLable];
Initialize the data on the lable
SELF.FIRSTSTR = @ "1";
SELF.SECONDSTR = @ "1";
}
#pragma Mark Uipickerviewdelegate
Pickerview Number of columns
-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{
return 2;
}
Returns the number of elements in each column
-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component{
if (component = = 0) {
return self.firstList.count;
}
return self.secondList.count;
}
Returns the width of each column
-(CGFloat) Pickerview: (Uipickerview *) Pickerview widthforcomponent: (Nsinteger) component{
return 50;
}
Returns the elements of each row in each column
-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component{
if (component = = 0) {
return Self.firstlist[row];
} else {
return Self.secondlist[row];
}
}
Use the string assignment string property of the selected row in the array for lable display
-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component{
if (component = = 0) {
Self.firststr = Self.firstlist[row];
} else {
Self.secondstr = Self.secondlist[row];
}
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
/*
#pragma mark-navigation
In a storyboard-based application, you'll often want to do a little preparation before navigation
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {
Get The new view controller using [Segue Destinationviewcontroller].
Pass the selected object to the new view controller.
}
*/
@end
Basic use of the Uipickerview simple selector