Usage of Pickerview in iOS

Source: Internet
Author: User

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

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.