iOS 11th day (1:uipickerview order)

Source: Internet
Author: User

Uipickerview

#import "ViewController.h"@interfaceViewcontroller () <UIPickerViewDataSource,UIPickerViewDelegate>@property (nonatomic,strong) Nsarray*Foods, @property (weak, nonatomic) Iboutlet UILabel*Fruitlabel, @property (weak, nonatomic) Iboutlet UILabel*Mainfoodlabel, @property (weak, nonatomic) Iboutlet UILabel*Drinklabel;-(ibaction) Randommenu; @property (weak, nonatomic) Iboutlet Uipickerview*Pickerview;@end@implementationViewcontroller-(Nsarray *) foods{if(!_foods) {NSString*filepath = [[NSBundle mainbundle] Pathforresource:@"foods.plist"Oftype:nil]; _foods=[Nsarray Arraywithcontentsoffile:filepath]; }        return_foods;}- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.NSLog (@"%@", Self.foods); //default selected data, first row of data//[self Pickerview:nil didselectrow:0 incomponent:0];//[self Pickerview:nil didselectrow:0 incomponent:1];//[self Pickerview:nil didselectrow:0 incomponent:2];Nsinteger Columns=Self.foods.count;  for(inti =0; i < columns; i++) {[Self Pickerview:nil didselectrow:0INCOMPONENT:I]; }        }#pragmaMark Uipickerview Data source#pragmaMark Number of Columns-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{returnSelf.foods.count;}#pragmaMark number of rows per column-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component{//get the data for each columnNsarray *items =Self.foods[component]; //returns the number of each column    returnItems.Count;}#pragmaMark Uipickerview Agent-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component{//get the data for each columnNsarray *items =Self.foods[component]; //returns the data for the row of the corresponding column    returnItems[row];}#pragmaMark Pickerview's Selection-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component{NSLog (@"component%ld Row%ld", Component,row); //Get DataNsarray *items =Self.foods[component]; NSString*rowstr =Items[row]; //Change Data    Switch(component) { Case 0://FruitSelf.fruitLabel.text =Rowstr;  Break;  Case 1://Staple FoodSelf.mainFoodLabel.text =Rowstr;  Break;  Case 2://BeveragesSelf.drinkLabel.text =Rowstr;  Break; default:             Break; }    }#pragmaMark Random menu, determined by the day what we eat--(ibaction) randommenu {NSLog (@"xx"); //1. Get how many rows are in each columnNsinteger columns =Self.foods.count;  for(inti =0; i < columns; i + +) {Nsarray*items =Self.foods[i]; //number of rows for the corresponding columnNsinteger Rowsincolumn=Items.Count; //2. Generate random data in line rangeNsinteger Randomrow =arc4random_uniform (int) rowsincolumn); //the random number of rows per column cannot be the same as before//gets the old number of rowsNsinteger Oldrow =[Self.pickerview selectedrowincomponent:i];  while(Oldrow = =Randomrow) {Randomrow=arc4random_uniform ((int) rowsincolumn); }                //0.0~0.9//Arc4random_uniform (Ten) * 0.1//0.00~0.99//Arc4random_uniform (+) * 0.01//3. Change Data//3.1 Changing the label's data[self Pickerview:nil didselectrow:randomrow incomponent:i]; #warningTo change the Pickerview selected row, use one of the Pickerview methods//3.1 Changing the Uipickerview selected data[Self.pickerview selectrow:randomrow incomponent:i Animated:yes]; }  }@end

iOS 11th day (1:uipickerview order)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.