Use the Picker view control to complete a simple meal selection application

Source: Internet
Author: User

Use the Picker view control to complete a simple meal selection application

First, the realization effect

  Description: Click the Random button, can be automatically selected, the data is automatically refreshed below.

  

Second, the realization of ideas

1.picker view has a default height of 162 and cannot be modified. 2. Display the data, you need to set up a data source, there are two ways (become a data source, Compliance Agreement) 3. Implement two methods within the data source 1) Returns the total number of columns 2) in this column, the total number of rows 4. Tell it through the proxy which row of that column shows which data (set its proxy as Controller) 5. Load all food with lazy loading 6. Complete the presentation of basic data (columns, rows, contents) 7. Automatically update the selected food letter Interest. (Use a large view, put 6 labels on it) 1) Assign 3 labs, add three properties (fruit, main course, drink) 2) listen to which line is selected (listening has two ideas, one is agent, one is notification), First look at the method that has no proxy (Didselectrow) This method is called when a row is selected, and the selected column number and line number are passed in as parameters. Can get the corresponding column number and line number. 3) The listener method that is called when the selection is completed 4) set the default selection in Viewdidload, set to [0][1] 5) Increased scalability (manually calling those lines-using a For loop) 8. Implementation of the Random function 1) How to let the code select a row (SelectRow), call the method can specify which row to scroll to that column 2) to achieve the function of the head (using a large uiview, which puts two child controls) 3) Set height 4 4, how to make the position of the random button centered? You can set its height to 44 and the maximum Y value to 64. 4) Set the Random button's Click event Randomfood, let Pickerview actively select a row. 5) The method of generating the random number (the limit of generating random number, no more than the current total) 6) disadvantage, in the future after the data changes, will be error (modulo in a few) [Self.foods[0] count]? Why not use shorthand syntax? (Remember to keep in mind) 7) random number processing is not rigorous, sometimes generated random number may be equal, then the column will not scroll, get to the corresponding column of the total number of data, how to get the last generated random value (that is, the currently selected row), compare the last line number and the current generated random number is the same, If the same, rewrite the build 9. To solve another problem, the following data random refresh is invalid, select a row by code. iii. Implementing code examples1. Project document structure and storyboard file Storyboard file large interface settings: 2. Code sample host controller file code:
   9 #import "YYViewController.h" 10 11//compliance with Data source and proxy protocol @interface Yyviewcontroller () <uipickerviewdatasource,uipicker viewdelegate> 13/** 14 * Fruit * * @property (strong, nonatomic) Iboutlet UILabel *fruitlab; 17/** 18 * Main Course * * @property (strong, nonatomic) Iboutlet UILabel *staplelab; 21/** 22 * Beverage * * * @property (strong, nonatomic) Iboutlet UILabel *drinklab; 25/** 26 * Save ALL data * * * @property (Nonatomic,strong) Nsarray *foods; @property (Weak, nonatomic) Iboutlet Uipickerview *pickerview; -(Ibaction) Randomfood: (ID) sender;      @end @implementation Yyviewcontroller-(void) viewdidload Notoginseng {[Super viewdidload]; 39 40 Here the initial display of the Data refresh section below is set to (int component = 0; component<self.foods.count; component++) {[Self pi] Ckerview:nil didselectrow:0 Incomponent:component]; The #pragma mark-uses lazy loading to load the data into the Nsarray (*) foods (_foods==nil) {NSString *fullpatH=[[nsbundle mainbundle]pathforresource:@ "Foods.plist" oftype:nil]; Wuyi Nsarray *arraym=[nsarray Arraywithcontentsoffile:fullpath]; _foods=arraym; +-----return _foods;  #pragma mark-handling Random button click event (ibaction) Randomfood: (ID) Sender {59 60//Let Pickerview actively select a row 61// Let Pickerview select the row line of incomponent column//[Self.pickerview selectrow:1 incomponent:0 Animated:yes]; */* [Self.pickerview selectrow:arc4random ()% incomponent:0 Animated:yes]; [Self.pickerview selectrow:arc4random ()% incomponent:1 Animated:yes]; [Self.pickerview selectrow:arc4random ()% incomponent:2 Animated:yes]; */[Self.foods objectatindex:0]; = = Self.foods[0]; +//[self.foods[0] count]; 72 73/* 74//Generate random values based on the number of elements in each column [Self.pickerview selectrow:arc4random ()% [self.foods[0] count] InCo mponent:0 Animated:yes]; [Self.pickerview Selectrow:arc4random ()% [Self.foods[1] count] incomponent:1 Animated:yes]; [Self.pickerview selectrow:arc4random ()% [self.foods[2] count] incomponent:2 Animated:yes];         78 */79 80//Set a random number of Bayi for (int component=0; component<self.foods.count; component++) {82 Gets the number of data elements that correspond to the current column. Total=[self.foods[component int] count]; 84//Generate random numbers based on the total number of each column (the currently generated random number) Randomnumber=arc4random int ()%total; 86 87//Gets the currently selected row (the line that was moved to after the last random) Oldrow=[self.pickerview int (selectedrowincomponent:0]); 89 90//Compare the last line number and the currently generated random number are the same, regenerate the Oldrow==randomnumber Ndomnumber=arc4random ()%total; 93} 94 95//Let Pickerview scroll to a specified line [Self.pickerview selectrow:randomnumber incomponent: Component Animated:yes]; 97//simulation, select a line by Code 98 [self Pickerview:nil didselectrow:randomnumber incomponent:component]; }100}101 102 #pragma mark-Set data 103//altogetherHow many columns 104-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerView105 {106 return self.foods.count;107} 108 109//Each column corresponds to how many lines-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component111 {112//1. Gets the current column 113 Nsarray *araym= self.foods[component];114//2. Returns the number of rows corresponding to the current column. Return ARAYM.C ount;116}117 118//row corresponds to what data is displayed for each column 119-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) row Forcomponent: (Nsinteger) component120 {121//1. Gets the current column 122 Nsarray *araym= self.foods[component];123//2. Gets the current column corresponding to the Row Data 124 NSString *name=araym[row];125 return name;126}127 #pragma mark-settings data refresh below 129//When a row of Pickerview is selected Use 130//To pass the selected column number and line number as parameters 131//The Pickerview is called only when a row is selected by the finger (void): (Uipickerview *) Pickerview Didselectrow: ( Nsinteger) Row incomponent: (Nsinteger) component133 {134//Get corresponding column, data of corresponding row 135 NSString *name=self.foods[component][row] ; 136//Assignment 137 if (0==component) {138 Self.fruitlab.text=name;139}else if (1==component), 141 self.staplelab.text=name;142}else143 self. drinklab.text=name;144}145 146 #pragma mark-hidden status bar 147-(BOOL) prefersStatusBarHidden148 {149 return yes;150}151 @end

Iv. Important Additions

Notice why [Self.foods[0] count] is used in the code implementation; Instead of directly using the point syntax self.foods[0].count to take a value.

[Self.foods objectatindex:0]; = = self.foods[0];//The effect of the two sentences is equivalent, and the self call objectatindex:0 this method, return is an ID type of the universal pointer, its real type to the actual run time can be detected, so can not directly use Self.foods [0].count.

Use the Picker view control to complete a simple meal selection application

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.