A. Requirements1. Use Pickerview to make 3 meals (fruit, main course, drink) with Demo2. The selected meal is displayed in real time in the "Display area" 3. Provide a "random" button to randomly select menu items to matchB. Implementation steps1. Drag into a Pickerview 2. Configure DataSource and delegate with the controller
1 //Compliance with Uipickerviewdatasource,uipickerviewdelegate2 @interfaceViewcontroller () <uipickerviewdatasource, uipickerviewdelegate>3 4 /** Selectors*/5@property (Weak, nonatomic) Iboutlet Uipickerview *Pickerview;6 7 @end8 9 @implementationViewcontrollerTen One- (void) Viewdidload { A [Super Viewdidload]; - //additional setup after loading the view, typically from a nib. - the //Set DataSource -Self.pickerView.dataSource =Self ; - //Set Delegage -Self.pickerview.Delegate=Self ; +}3. Implementing Proxy Methods
1 #pragmaMark-Proxy method2 /** Number of columns*/3-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview {4 return 3;5 }6 7 /** Number of rows for the corresponding column*/8-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component {9 return 5;Ten } One A /** Corresponding columns and rows of the display content*/ --(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component { - return @"content"; the}4. Introduce the plist file to read the data
1 /** Load data, delay loading*/2-(Nsarray *) foodcategories {3 if(Nil = =_foodcategories) {4_foodcategories = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"foods.plist"Oftype:nil]];5 }6 7 return_foodcategories;8 }9 Ten #pragmaMark-datasource method One /** Number of columns*/ A-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview { - returnSelf.foodCategories.count; - } the - /** Number of rows for the corresponding column*/ --(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component { -Nsarray *foods =Self.foodcategories[component]; + returnFoods.count; - } + A at #pragmaMark-delegate method - /** Corresponding columns and rows of the display content*/ --(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component { -Nsarray *foods =Self.foodcategories[component]; - returnFoods[row]; -}5. Making the display area
1 /* */2 @property (weak, nonatomic) iboutlet UILabel *Fruitlabel; 3 /* */4 @property (weak, nonatomic) iboutlet UILabel *Mainlabel; 5 /* */6 @property (weak, nonatomic) Iboutlet UILabel *drinklabel;
6. Responding to selection events
1 /** Respond to select meal Events*/2- (void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component {3Nsarray *foods =Self.foodcategories[component];4NSString *food =Foods[row];5 6 Switch(component) {7 Case 0:8Self.fruitLabel.text =Food ;9 Break;Ten Case 1: OneSelf.mainLabel.text =Food ; A Break; - Case 2: -Self.drinkLabel.text =Food ; the Break; - default: - Break; -}7. Make "random" function
1 /** Randomly selected meals*/2-(ibaction) onrandomclicked {3 //cycle all meal types4 for(intI=0; i<self.foodcategories.count; i++) {5 //number of meal lines previously selected6Nsinteger Originalrow =[Self.pickerview selectedrowincomponent:i];7 8Nsinteger row =Originalrow;9 Ten //all meals included in this category OneNsarray *foods =Self.foodcategories[i]; A //if the random meal is the same as the previous one, continue randomly - while(Row = =Originalrow) { -row = Arc4random ()%Foods.count; the } - - //notify Pickerview to change the selection - [Self.pickerview selectrow:row incomponent:i Animated:yes]; + - //Change the display area + [self PickerView:self.pickerView didselectrow:row incomponent:i]; A } at}
[iOS base Control-6.10.1] Pickerview Meal with Demo