Association selection of UIPickerView Control for iOS Learning

Source: Internet
Author: User

Next, the simple use of the UIPickerView Control for iOS

Next on the Code http://download.csdn.net/detail/totogo2010/4391870, we want to achieve the following effect:



When the level-1 option on the left is selected, the level-2 option in the level-1 option is displayed on the left, and the option is displayed in TextField.

How to implement it? Create an array corresponding to the list key on the left. When this key is selected, refresh the content of the UIPickerView on the left to display the data of the corresponding array.

Two UIPickerView parts, rowIndex, locate the data and put it in TextField.

1. Open the PickerViewDemo project in the previous article and add two member variables in ViewController. h: NSArray * subPickerArray; NSDictionary * dicPicker;

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

2. Initialization

-(Void) viewDidLoad {[super viewDidLoad]; pickerArray = [NSArray arrayWithObjects: @ "animal", @ "plant", @ "Stone", nil]; dicPicker = [NSDictionary dictionaryWithObjectsAndKeys: [NSArray arrayWithObjects: @ "fish", @ "bird", @ "worm", nil], @ "animal", [NSArray arrayWithObjects: @ "Flowers", @ "Grass", @ "sunflower", nil], @ "Plants", [NSArray arrayWithObjects: @ "Crazy Stone", @ "granite ", @ "pebbles", nil], @ "Stone", nil]; subPickerArray = [dicPicker objectForKey: @ "animal"]; textField. inputView = selectPicker; textField. inputAccessoryView = donatelbar; textField. delegate = self; selectPicker. delegate = self; selectPicker. dataSource = self; selectPicker. frame = CGRectMake (0,480,320,216 );}

Assign a value to NSDictionary * dicPicker; and add the corresponding array to the three keywords.

3. Component returns two gears.
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 2;}
4. Use macros

Under # import "ViewController. h", two macros are defined to represent the left part and the right part of the UIPickerView gear. The left part is 0, and the right part is 1.

# Import "ViewController. h"

# Define kFirstComponent 0

# Define kSubComponent 1

5. Determine the gear and return the Count of the corresponding data.

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    if(component == kFirstComponent){        return [pickerArray count];    }else {        return [subPickerArray count];    }}

6. Return corresponding String Data Based on component

    -(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{    if(component == kFirstComponent){        return [pickerArray objectAtIndex:row];    }else {        return [subPickerArray objectAtIndex:row];    }}

7. When the gear on the left is dragged, the data on the right is reloaded accordingly.

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{    if (component == kFirstComponent) {        subPickerArray = [dicPicker objectForKey:[pickerArray objectAtIndex:row]];         [pickerView selectRow:0 inComponent:kSubComponent animated:YES];        [pickerView reloadComponent:kSubComponent];    }}

8. The selected data is displayed on TextField.

-(Void) Authorization :( UITextField *) textField {NSInteger firstViewRow = [selectPicker failed: Failed]; NSInteger subViewRow = [selectPicker failed: kSubComponent]; NSString * firstString = [pickerArray objectAtIndex: firstViewRow]; NSString * subString = [[dicPicker objectForKey: [pickerArray objectAtIndex: firstViewRow] objectAtIndex: subViewRow]; NSString * textString = [[NSString alloc] initWithFormat: @ "you selected: % @", firstString, @ "in", subString]; self. textField. text = textString;}-(IBAction) selectButton :( id) sender {[textField endEditing: YES];}

Success! Run. Click TextField to bring up:

Final code: http://download.csdn.net/detail/totogo2010/4393004

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.