Today is a special day (Mac Pro knocks on the cool ... Arrived yesterday.)
////QRVIEWCONTROLLER.M//#import "QRViewController.h"@interfaceQrviewcontroller () <UIPickerViewDataSource,UIPickerViewDelegate>@property (Weak, nonatomic) Iboutlet UILabel*fruitlable;//Fruit@property (Weak, nonatomic) Iboutlet UILabel *mainlabel;//Main Course@property (Weak, nonatomic) Iboutlet UILabel *drinklabel;//Beverages@property (nonatomic,strong) Nsarray *Foods, @property (weak, nonatomic) Iboutlet Uipickerview*Pickerview;-(ibaction) RanDom;@end@implementationQrviewcontroller- (void) viewdidload {[Super viewdidload]; for(intI=0; i<self.foods.count; i++) {[Self Pickerview:nil didselectrow:0INCOMPONENT:I]; } //[self Pickerview:nil didselectrow:0 incomponent:0]; //[self Pickerview:nil didselectrow:0 incomponent:1]; //[self Pickerview:nil didselectrow:0 incomponent:2];}/** Lazy Loading data*/-(Nsarray *) foods{if(_foods==Nil) {NSString*path=[[nsbundle Mainbundle] Pathforresource:@"Foods"OfType:@"plist"]; Nsarray*arraylist=[Nsarray Arraywithcontentsoffile:path]; _foods=ArrayList; } return_foods;}#pragmaMark-Data Source methods/** * How many columns?*/-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{returnSelf.foods.count;}/** * How many rows are displayed in a column*/-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component{Nsarray*subfoods=Self.foods[component]; returnSubfoods.count;}#pragmaMark-Proxy method/** * Data displayed on a row in a column*/-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component{returnSelf.foods[component][row];}/** * Select a row and a column*/- (void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (nsinteger) component{if(component==0){//Fruitself.fruitlable.text=Self.foods[component][row]; }Else if(component==1){//Main Courseself.mainlabel.text=Self.foods[component][row]; }Else if(component==2){//Beveragesself.drinklabel.text=Self.foods[component][row]; }}/** * Randomly generate a*/-(ibaction) ranDom { for(intI=0; i<self.foods.count; i++) { //the total length of each line intCount=[Self.foods[i] count]; //generate a random number intRow=arc4random ()%count; //set the row for the Pickerview selected column[Self.pickerview selectrow:row incomponent:i Animated:yes]; //set the text of a label[self Pickerview:nil didselectrow:row incomponent:i]; } }@end
Usage of Pickerview in iOS