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

Source: Internet
Author: User

iOS development UI Chapter-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:
////YYVIEWCONTROLLER.M//06-Implementation of a simple vegetable selection system////Created by Apple on 14-6-5.//Copyright (c) 2014 itcase. All rights reserved.//#import"YYViewController.h"//compliance with data sources and proxy protocols@interface Yyviewcontroller () <UIPickerViewDataSource,UIPickerViewDelegate>/** * Fruit*/@property (Strong, nonatomic) Iboutlet UILabel*Fruitlab;/** Main Course*/@property (Strong, nonatomic) Iboutlet UILabel*Staplelab;/** * Drinks*/@property (Strong, nonatomic) Iboutlet UILabel*Drinklab;/** * Save all the data*/@property (nonatomic,strong) Nsarray*Foods, @property (weak, nonatomic) Iboutlet Uipickerview*Pickerview;-(ibaction) Randomfood: (ID) sender; @end @implementation Yyviewcontroller- (void) viewdidload{[Super Viewdidload]; //Here you set the initial display of the Data refresh section below     for(intComponent =0; component<self.foods.count; component++) {[Self Pickerview:nil didselectrow:0Incomponent:component]; }}#pragmamark-uses lazy loading to load the data in-(Nsarray *) foods{if(_foods==Nil) {NSString*fullpath=[[nsbundle Mainbundle]pathforresource:@"foods.plist"Oftype:nil]; Nsarray*arraym=[Nsarray Arraywithcontentsoffile:fullpath]; _foods=Arraym; }    return_foods;}#pragmamark-handling Random button click Events-(ibaction) Randomfood: (ID) Sender {//let Pickerview actively select a row//let Pickerview select Row row for 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];        /*generate random values based on the number of elements in each column [Self.pickerview selectrow:arc4random ()% [self.foods[0] count] incomponent:0 Animated:ye     S];     [Self.pickerview selectrow:arc4random ()% [self.foods[1] count] incomponent:1 Animated:yes];     [Self.pickerview selectrow:arc4random ()% [self.foods[2] count] incomponent:2 Animated:yes]; */        //set a random number     for(intComponent=0; component<self.foods.count; component++) {        //gets the number of data elements that correspond to the current column        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 ; //gets the currently selected row (the row that was last randomly moved to)        intOldrow=[self.pickerview selectedrowincomponent:0]; //compares the last line number to the current generated random number, and rebuilds if the same         while(oldrow==Randomnumber) {Randomnumber=arc4random ()%Total ; }                //let Pickerview scroll to a specified line[Self.pickerview selectrow:randomnumber incomponent:component Animated:yes]; //simulate, select a row by code[self Pickerview:nil didselectrow:randomnumber incomponent:component]; }}#pragmamark-Setting up data//How many columns?-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{returnSelf.foods.count;}//How many rows each column corresponds to-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component{//1. Get the current columnNsarray *araym=Self.foods[component]; //2. Returns the number of rows corresponding to the current column    returnAraym.count;}//what data is displayed for each row in each column-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component{//1. Get the current columnNsarray *araym=Self.foods[component]; //2. Get the data for the row that corresponds to the current columnNSString *name=Araym[row]; returnname;}#pragmamark-Setting the data refresh below//called when a line of Pickerview is selected//The selected column and line numbers are passed as parameters//It is only called when a row is selected by the finger-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (nsinteger) component{//gets the corresponding column, corresponding to the row's dataNSString *name=Self.foods[component][row]; //Assign Value    if(0==component) {Self.fruitLab.text=name; }Else if(1==component) {Self.stapleLab.text=name; }ElseSelf.drinkLab.text=name;}#pragmamark-Hide Status Bar-(BOOL) prefersstatusbarhidden{returnYES;} @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.

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.