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.