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

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 protocol)

3. Two ways to implement a data source

1) Returns the total number of columns

2) How many lines in this column are there

4. Tell it by proxy which row in that column shows which data (set its proxy as Controller)

5. Load all the food with lazy loading

6. Complete the presentation of the basic data (column, line, content)

7. Automatically update the selected food information. (Use a large view with 6 labels on it)

1) Assign values to 3 labs and add three attributes (fruit, main course, drink)

2) monitor which line is selected (there are two ideas of listening, one is the agent, one is the notification), first look at the method with 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 invoked when the check 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 random functions

1) How to get the code to select a row (SelectRow), call this method to specify which row to scroll to that column

2) Implement the function of the head (using a large uiview with two sub-controls inside)

3) Set height 44, 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) method of generating random numbers (the limit for generating random numbers, no more than the current total)

6) shortcomings, in the future after the data changes, will be error (die 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 is overridden by the build

9. To solve another problem, the following data random refresh is invalid, select a row through the code.

Iii. Implementing code Examples

1. Project document structure and storyboard file

Storyboard file Large interface settings:

2. Code examples

Main 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//87 mImport"YYViewController.h "1011//Compliance with data sources and proxy protocols@interface Yyviewcontroller () <UIPickerViewDataSource,UIPickerViewDelegate>13/** 14 * Fruit 15 */16@property (Strong, nonatomic) Iboutlet UILabel *fruitlab;17/** 18 * Main Course 19 */20@property (Strong, nonatomic) Iboutlet UILabel *staplelab;21st/** 22 * Beverage 23 */24@property (Strong, nonatomic) Iboutlet UILabel *drinklab;25/** 26 * Save All data 27 */28@property (Nonatomic,strong) Nsarray *foods;29@property (Weak, nonatomic) Iboutlet Uipickerview *pickerview;-(Ibaction) Randomfood: (ID) sender;31@end33Yyviewcontroller @implementation3536-(void) Viewdidload37 {38 [Super Viewdidload];3940//Here you set the initial display of the Data refresh section below41for (int component =0; component<self.foods.count; component++) {[Self Pickerview:nil didselectrow:0 Incomponent:component];43}44}45#pragma mark-use lazy loading to load data in-(Nsarray *) foods48 {49if (_foods==nil) {NSString *fullpath=[[nsbundle mainbundle]pathforresource:@"Foods.plist "Oftype:nil";Wuyi Nsarray *arraym=[nsarray Arraywithcontentsoffile:fullpath];_foods=arraym;53}54return _foods;55}56mark-#pragma handle Click events for random buttons(ibaction) Randomfood: (ID) Sender {5960//Let Pickerview actively select a row61//Let Pickerview select Row row for incomponent column62//[Self.pickerview selectrow:1 incomponent:0 Animated:yes];6364/*[Self.pickerview selectrow:arc4random ()% incomponent:0 Animated:yes]; [Self.pickerview selectrow:arc4random ()% incomponent:1 Animated:yes]; [Self.pickerview selectrow:arc4random ()% incomponent:2 Animated:yes]; 68 */6970//[Self.foods objectatindex:0]; = = Self.foods[0];71//[Self.foods[0] count];7273/*74//Generate random values based on the number of elements in each column [Self.pickerview selectrow:arc4random ()% [self.foods[0] count] incomponent: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 */7980//Set a random number81for (int component=0; component<self.foods.count; component++) {82//Gets the number of data elements that correspond to the current column83int total=[self.foods[component] count];84//Generates a random number based on the total number of each column (the currently generated random number)85int randomnumber=arc4random ()%total;8687//Gets the currently selected row (the row that was last randomly moved to)88int Oldrow=[self.pickerview selectedrowincomponent:0];8990//Compares the last line number to the current generated random number, and rebuilds if the same91while (Oldrow==randomnumber) {Randomnumber=arc4random ()%total;93}9495//Let Pickerview scroll to a specified line[Self.pickerview selectrow:randomnumber incomponent:component Animated:yes];97//Simulate, select a row by code98 [Self Pickerview:nil didselectrow:randomnumber incomponent:component];99}100}101102 Setting Data #pragma mark-103//How many columns?104-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview105 {106return self.foods.count;107}108109//How many rows each column corresponds to-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component111 {112//1. Get the current column113 Nsarray *araym= Self.foods[component];114//2. Returns the number of rows corresponding to the current column115return araym.count;116}117118//What data is displayed for each row in each column119-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (NSInteger) component120 {121//1. Get the current column122 Nsarray *araym= self.foods[component];123//2. Get the data for the row that corresponds to the current column124 NSString *name=araym[row];125return name;126}127Data refresh below #pragma mark-settings129//Called when a line of Pickerview is selected130//The selected column and line numbers are passed as parameters131//It is only called when a row is selected by the finger132-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component133 {134//Gets the corresponding column, corresponding to the row's data135 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;  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.

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

Related Article

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: 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.