iOS development UI Chapter-use the Picker view control to complete a simple meal selection application

Source: Internet
Author: User

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:
1 //2 //YYVIEWCONTROLLER.M3 //06-Implementation of a simple vegetable selection system4 //5 //Created by Apple on 14-6-5.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten  One //compliance with data sources and proxy protocols A @interfaceYyviewcontroller () <UIPickerViewDataSource,UIPickerViewDelegate> - /** - * Fruit the  */ -@property (Strong, nonatomic) iboutlet UILabel *Fruitlab; - /** - * Main Course +  */ -@property (Strong, nonatomic) iboutlet UILabel *Staplelab; + /** A * Beverage at  */ -@property (Strong, nonatomic) iboutlet UILabel *Drinklab; - /** - * Save all the data -  */ -@property (nonatomic,strong) Nsarray *Foods; in@property (Weak, nonatomic) Iboutlet Uipickerview *Pickerview; --(Ibaction) Randomfood: (ID) sender; to  + @end -  the @implementationYyviewcontroller *  $- (void) ViewdidloadPanax Notoginseng { - [Super Viewdidload]; the      +     //Here you set the initial display of the Data refresh section below A      for(intComponent =0; component<self.foods.count; component++) { the[Self Pickerview:nil didselectrow:0Incomponent:component]; +     } - } $  $ #pragmamark-use lazy loading to load the data --(Nsarray *) Foods - { the     if(_foods==Nil) { -NSString *fullpath = [[NSBundle mainbundle] Pathforresource:@"foods.plist"Oftype:nil];WuyiNsarray *arraym =[Nsarray Arraywithcontentsoffile:fullpath]; the_foods =Arraym; -     } Wu     return_foods; - } About  $ #pragmamark-Handle click events for random buttons --(Ibaction) Randomfood: (ID) Sender { -      -     //let Pickerview actively select a row A     //let Pickerview select Row row for incomponent column +     //[Self.pickerview selectrow:1 incomponent:0 Animated:yes]; the      -     /* $ [Self.pickerview selectrow:arc4random ()% incomponent:0 Animated:yes]; the [Self.pickerview selectrow:arc4random ()% incomponent:1 Animated:yes]; the [Self.pickerview selectrow:arc4random ()% incomponent:2 Animated:yes]; the      */ the      -     //[Self.foods objectatindex:0]; = = Self.foods[0]; in     //[Self.foods[0] count]; the      the     /* About //Generate random values based on the number of elements in each column the [Self.pickerview selectrow:arc4random ()% [self.foods[0] count] incomponent:0 Animated:yes]; the [Self.pickerview selectrow:arc4random ()% [self.foods[1] count] incomponent:1 Animated:yes]; the [Self.pickerview selectrow:arc4random ()% [self.foods[2] count] incomponent:2 Animated:yes]; +      */ -      the     // set a random numberBayi      for(intComponent =0; Component < Self.foods.count; component++) { the         // gets the number of data elements that correspond to the current column the         intTotal =[Self.foods[component] count]; -         // generates a random number based on the total number of each column (the currently generated random number) -         intRandomnumber = arc4random ()%Total ; the          the         // gets the currently selected row (the row that was last randomly moved to) the         intOldrow = [Self.pickerview selectedrowincomponent:component]; the          -         // compares the last line number to the current generated random number, and rebuilds if the same the          while(Oldrow = =Randomnumber) { theRandomnumber = arc4random ()%Total ; the         }94          the         // let Pickerview scroll to a specified line the [Self.pickerview selectrow:randomnumber incomponent:component Animated:yes]; the         // simulate, select a row by code98 [self Pickerview:nil didselectrow:randomnumber incomponent:component]; About     } - }101 102 #pragmaMark-Set data103 // How many columns?104-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview the {106     returnSelf.foods.count;107 }108 109 // How many rows each column corresponds to the-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component111 { the     // 1. Get the current column113Nsarray *araym =Self.foods[component]; the     //2. Returns the number of rows corresponding to the current column the     returnAraym.count; the }117 118 // what data is displayed for each row in each column119-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component - {121     // 1. Get the current column122Nsarray *araym =Self.foods[component];123     // 2. Get the data for the row that corresponds to the current column124NSString *name =Araym[row]; the     returnname;126 }127  - #pragmamark-Setting the data refresh below129 //called when a line of Pickerview is selected the //The selected column and line numbers are passed as parameters131 //It is only called when a row is selected by the finger the-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (nsinteger) component133 {134     // gets the corresponding column, corresponding to the row's data135NSString *name =Self.foods[component][row];136     // Assign Value137     if(0==component) {138Self.fruitLab.text =name;139}Else if(1==component) $     {141Self.stapleLab.text =name;142}Else143Self.drinkLab.text =name;144 }145 146 #pragmamark-Hide Status bar147-(BOOL) Prefersstatusbarhidden148 {149     returnYES; Max }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 be detected when the actual operation, so You cannot use Self.foods[0].count directly.

iOS development UI Chapter-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.