iOS development à la carte system using Uipickerview

Source: Internet
Author: User

Use Uipickerview to implement a simple à la carte interface as shown in. Source Address: Https://github.com/xiaoLong1010/iOSAppDemo.git

Uipickerview has two protocol Uipickerviewdatasource,uipickerviewdelegate, which provide data and proxies for Uipickeview. Uipickeview's three component represent Fruit,main,drink, select a row, and the menu name is shown below. At the top of the random button, a random à la carte function is implemented.

1.viewController implements two protocols and references related data and controls.

@interface Csyviewcontroller () <UIPickerViewDataSource,UIPickerViewDelegate>** * * *  **randbtn; @end

2. The data is stored in the plist file, referenced by Foodgroup, and read with lazy loading.

// Override getter method, lazy load implementation -(Nsarray *) foodgroups{    if (!  _foodgroups) {        *path = [[NSBundle mainbundle] Pathforresource:@ "foods.plist" Oftype:nil];                 = [Nsarray arraywithcontentsoffile:path];    }         return _foodgroups;    }

Information provided by 3.UIPickerViewDataSource: Number of component and how many rows per component

#pragma Mark--Uipickerviewdatasource// return component number -(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{    return  Self.foodGroups.count;} // returns the number of rows per component -(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: ( Nsinteger) component{    return  [self.foodgroups[component] count];}

4.UIPickerViewDelegate provides the data displayed for each row.

#pragma Mark--uipickerviewdelegate// each row displays the data, string data -(NSString *) Pickerview: ( Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component{    return  Self.foodgroups[component][row];}

5. After you select a row in Uipickerview, you need to dynamically change the menu name below. This is done by the Didselectrow: method of the Uipickerviewdelegate protocol, which is automatically called by a row in the selected line. That is, changing the name of the dish dynamically in the method.

- (void) viewdidload{[Super Viewdidload];//For (Nsarray *group in self.foodgroups) {//Csylog (@ "%@", group);//    }        //initialize three label, menu name     for(inti =0; i < Self.foodGroups.count; i++) {[Self pickerView:self.picker didselectrow:4INCOMPONENT:I]; }    //Nsinteger row = [Self.picker selectedrowincomponent:0];//Csylog (@ "Selected row =%d", row); //Monitoring Methods[self.randbtn addtarget:self Action: @selector (Randbtndidclick:) forcontrolevents:uicontroleventtouchupinside]        ; }#pragmaMark--randbtn listener-(void) Randbtndidclick: (UIButton *) button{//Csylog (@ "click");         for(inti =0; i < Self.foodGroups.count; i++) {Nsarray*group =Self.foodgroups[i]; Nsinteger NewRow= Arc4random ()%Group.count; //rows that were previously selectedNsinteger CurrentRow =[Self.picker selectedrowincomponent:i];//NSLog (@ "CurrentRow =%d", currentrow); //if the random value is equal to the current value, the random value adds 1 .        if(NewRow = =CurrentRow) {NewRow= (NewRow +1) %Group.count; }//NSLog (@ "NewRow =%d", newRow); //Select a row of a column[Self.picker selectrow:newrow incomponent:i Animated:yes]; //Call the Didsecled method, update three labels[self pickerView:self.picker didselectrow:newrow incomponent:i]; }        }

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.