iOS Development UI Chapter-uipickerview Controls Brief introduction

Source: Internet
Author: User

iOS Development UI Chapter-uipickerview Controls Brief introduction

One. Uipickerview Controls

1. Brief Introduction:

2. Sample Code

TXVIEWCONTROLLER.M file

Created by Xin on 14-10-15.

Copyright (c) 2014 Liang. All rights reserved.

//

#import "TXViewController.h"

@interface Txviewcontroller () <UIPickerViewDataSource,UIPickerViewDelegate>

@property (Weak, nonatomic) Iboutlet Uipickerview *pickerview;

-(ibaction) Randomfood;

@property (nonatomic, strong) Nsarray *foods;

@property (Weak, nonatomic) Iboutlet UILabel *fruitlabel;

@property (Weak, nonatomic) Iboutlet UILabel *mainlabel;

@property (Weak, nonatomic) Iboutlet UILabel *drinklabel;

@end

@implementation Txviewcontroller

-(void) viewdidload

{

[Super Viewdidload];

Additional setup after loading the view, typically from a nib.

Initialization

Self.fruitLabel.text = self.foods[0][0];

Self.mainLabel.text = self.foods[1][0];

Self.drinkLabel.text = self.foods[2][0];

Call the proxy method manually so it can be used for loop, not above

[Self Pickerview:nil didselectrow:0 incomponent:0];

[Self Pickerview:nil didselectrow:0 incomponent:1];

[Self Pickerview:nil didselectrow:0 incomponent:2];

for (int Component = 0; Component < Self.foods.count; component++) {

[Self Pickerview:nil didselectrow:0 incomponent:component];

}

}

Randomly select a food

-(ibaction) Randomfood {

Generate random numbers

Arc4random ()%14; Generates a random number from 0 to 14, [Self.foods[0] count] takes out the No. 0 column of this array, and this array calls the Count method

[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:2animated:yes];

Use for loop

for (int component = 0; component < Self.foods.count; component++) {

The total length of the component column array

int count = [Self.foods[component]count];

The previous line number

int oldrow = [Self.pickerview selectedrowincomponent:component];

The first few lines the default new line number is the same as the old line number

int row =oldrow;

Arc4random ()%count equivalent Arc4random_uniform (count);

Make sure the number of rows is different

The previous line number

while (row ==oldrow) {

row = Arc4random ()%count;

}

Let Pickerview actively select column component row

[Self.pickerview selectrow:row incomponent:component Animated:yes];

Set the text of a label

[Self Pickerview:nil didselectrow:row incomponent:component];

}

}

-(Nsarray *) foods

{

if (_foods = = nil) {

_foods = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle]pathforresource:@ "Foods" ofType:@ "plist"]];

}

return _foods;

}

#pragma mark---data source method

How many columns?

-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview

{

return self.foods.count;

}

Column. (Nsinteger) component (Nsinteger) Componentl column shows how many rows

-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component

{

First remove the array from column component

Nsarray *subfoods = self.foods[component];

return subfoods.count;

}

#pragma mark--proxy method

-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component

{

First, take out the component column, and in this row of the array

return Self.foods[component][row];

}

Check column component row

-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component

{

NSLog (@ "Selected%d column%d", component, row);

if (component ==0) {

Self.fruitLabel.text = Self.foods[component][row];

}

else if (component ==1)

{

Self.mainLabel.text = Self.foods[component][row];

}

else if (component = = 2)

{

Self.drinkLabel.text = Self.foods[component][row];

}

}

@end

Implementation results:

two. Uipickerview usage1. Common Properties of Uipickerview//data source (used to tell Uipickerview how many columns there are rows)@property (nonatomic,assign)ID<UIPickerViewDataSource>DataSource;//agent (used to tell Uipickerview what to display on every 1 rows of every 1 columns, listen for Uipickerview selection)@property (nonatomic,assign)ID<UIPickerViewDelegate>Delegate;//do you want to display the selected indicator@property (nonatomic) BOOL showsselectionindicator;//How many columns are there altogether?@property (Nonatomic,ReadOnly) Nsinteger numberofcomponents;2. Common methods of Uipickerview//re-refresh all columns- (void) reloadallcomponents;//re-refresh column component- (void) Reloadcomponent: (Nsinteger) component;//the row row of column component is actively selected- (void) SelectRow: (Nsinteger) Row incomponent: (Nsinteger) component animated: (BOOL) animated;//get the currently selected line number in column component-(Nsinteger) Selectedrowincomponent: (Nsinteger) component;3. Data source Method (Uipickerviewdatasource)//How many columns are there altogether?-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview;//How many lines are there in column component?-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component;4. Proxy method (Uipickerviewdelegate)//What is the width of column component?-(CGFloat) Pickerview: (Uipickerview *) Pickerview widthforcomponent: (nsinteger) component;//What is the row height of column component ?-(CGFloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (nsinteger) component;//what text is displayed in column component row-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component;//section component row row shows what view (content)-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component Reusingview: (UIView *) view;//column component row of Pickerview was selected- (void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component;

iOS Development UI Chapter-uipickerview Controls Brief introduction

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