Simple use of UIPickerView Control for iOS Learning

Source: Internet
Author: User

The UIPickerView control is often used when you select certain data for the user. Here, a simple selection data is displayed. In the UITextField input box, UIPickerView is used as the input View, use Toolbar as the button to select data. Similar to other UITableView controls, UIPickerView also requires a data source.

We want to achieve the following results:

The following procedure is used.

1. Open XCode 4.3.2 and create a new Single View Application named PickerViewDemo, Company Identifier: com. rongfzh. yc2, and drag and drop controls.

2.1 drag and drop a UIPickerView and place it at the bottom of the View

2.2 drag and drop a Toolbar control and place it outside the View to make it not a child control of the View and name the item "finish". The effect is as follows:

 

2.3 place a Flexible Space Bar Button Item to open it

 

2.4 put a UITextField to display the selected data

 

3. Create a ing

In the ViewController. xib file, press alt + command + enter to open the Assistant Editor, press and hold the Control key, select various controls, and drag them to the ViewController. h file to generate the following variable code:

 

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    NSArray *pickerArray;}- (IBAction)selectButton:(id)sender;@property (strong, nonatomic) IBOutlet UIToolbar *doneToolbar;@property (strong, nonatomic) IBOutlet UIPickerView *selectPicker;@property (strong, nonatomic) IBOutlet UITextField *textField;@end

 

4. implement data sources and protocols

ViewController. h file implementation

<UIPickerViewDelegate, UITextFieldDelegate, UIPickerViewDataSource>

 

 

#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIPickerViewDelegate, UITextFieldDelegate,UIPickerViewDataSource>{    NSArray *pickerArray;}- (IBAction)selectButton:(id)sender;@property (strong, nonatomic) IBOutlet UIToolbar *doneToolbar;@property (strong, nonatomic) IBOutlet UIPickerView *selectPicker;@property (strong, nonatomic) IBOutlet UITextField *textField;@end

ViewController. m file

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 1;}-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    return [pickerArray count];}-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{    return [pickerArray objectAtIndex:row];}-(void)textFieldDidEndEditing:(UITextField *)textField{    NSInteger row = [selectPicker selectedRowInComponent:0];    self.textField.text = [pickerArray objectAtIndex:row];}

 

The preceding numberOfComponentsInPickerView returns several pickerviews,

TextFieldDidEndEditing: When textField finishes editing, the data selected by PickerView is displayed.

 

The completion button of the item in the Toolbar.

 

- (IBAction)selectButton:(id)sender {    [textField endEditing:YES];}

 

 

5. Initialization

 

 

-(Void) viewDidLoad {[super viewDidLoad]; pickerArray = [NSArray arrayWithObjects: @ "animal", @ "plant", @ "Stone", @ "sky", nil]; textField. inputView = selectPicker; textField. inputAccessoryView = donatelbar; textField. delegate = self; selectPicker. delegate = self; selectPicker. dataSource = self; selectPicker. frame = CGRectMake (0,480,320,216 );}

 

Code explanation:

Set Delegation

TextField. delegate = self;

SelectPicker. delegate = self;

SelectPicker. dataSource = self;

 

Hide UIPickerView

SelectPicker. frame = CGRectMake (0,480,320,216 );

Run:

 

Example code: http://download.csdn.net/detail/totogo2010/4391870

Https://github.com/schelling/YcDemo

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you.
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.