Implement a Uipickerview on the interface:
1. Create a new singleviewapplication in the xxxviewcontroller.m file:
1 @interface moboviewcontroller ()
2 @property (nonatomic, Strong) Uipickerview *picker;
3 @end
2. In the-(void) Viewdidload method, implement:
-(void) viewdidload{ [Super Viewdidload]; // instantiation of Self.picker = [[Uipickerview alloc]init]; // location Self.picker.center = self.view.center; // Add to view [Self.view addSubview:self.picker];}
3, Cmd+r run up:
Discovery is blank! Why is it? We go into Uipickerview's head file to take a look, hold cmd Click Uipickerview, we found:
1 @protocol uipickerviewdatasource, uipickerviewdelegate;
Ha ha! is because we have not complied with TA's data source protocol and proxy Method!
4. We adhere to the data source and proxy protocol, following the @interface section of the controller.m file to comply with the agreement:
1 @interface moboviewcontroller () <UIPickerViewDelegate,UIPickerViewDataSource>// Compliance Agreement
2 @property (nonatomic, Strong) Uipickerview *picker;
3 @end
5. In-(void) Viewdidload We set the proxy and the data source to self:
1- (void) Viewdidload2 {3 [Super Viewdidload];4 5 //instantiation of6Self.picker =[[Uipickerview alloc]init];7 8 //Set up proxy9Self.picker.Delegate=Self ;Ten One //set up a data source ASelf.picker.dataSource =Self ; - - //location theSelf.picker.center =Self.view.center; - - //Add to view - [Self.view AddSubview:self.picker]; + -}
6. Implement data sources and proxy methods:
To return the number of groups, we'll go straight back to the 2 group.
1-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview2 {3 if([Pickerview IsEqual:self.picker]) {//If the incoming pickerview is just realized, Picker returns 2 .4 return 2;5 }6 7 return 0;//if not, return 0 .8}
Returns the number of rows per group of data, 10 a favor.
1-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component2 {3 if([Pickerview IsEqual:self.picker]) {//If the pickerview is just realized, picker returns4 return Ten;5 }6 7 return 0;//if not, return 0 .8}
1 /*2 Implementing Proxy Methods3 */4 5-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component6 {7 if([Pickerview IsEqual:self.picker]) {8 9 if(Component = =0) {Ten return[NSString stringWithFormat:@"A%d", row+1]; One } A Else if(Component = =1){ - return[NSString stringWithFormat:@"B%d", row+1]; - } the - } - return 0; -}
7, Cmd+r, run up!
Ios7_ about Uipickerview_ How to implement Uipickerview_ how to set the number of Uipickerview groups the number of rows per group of data