Association selection of IOS uipickerview Control
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. When selecting this, find the two uipickerview parts rowindex, find the data and put it in textfield.
We want to achieve the following results:
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;
[CPP] view plaincopy
1 # import <uikit/uikit. h>
2
3 @ interface viewcontroller: uiviewcontroller <uipickerviewdelegate, uitextfielddelegate, uipickerviewdatasource>
4 {
5 nsarray * pickerarray;
6 nsarray * subpickerarray;
7 nsdictionary * dicpicker;
8}
9-(ibaction) selectbutton :( ID) sender;
10 @ property (strong, nonatomic) iboutlet uitoolbar * donatelbar;
11 @ property (strong, nonatomic) iboutlet uipickerview * selectpicker;
12 @ property (strong, nonatomic) iboutlet uitextfield * textfield;
13 @ end
2. Initialization
[CPP] view plaincopy
14-(void) viewdidload
15 {
16 [Super viewdidload];
17 pickerarray = [nsarray arraywithobjects: @ "animal", @ "plant", @ "Stone", nil];
18 dicpicker = [nsdictionary dictionarywithobjectsandkeys:
19 [nsarray arraywithobjects: @ "fish", @ "bird", @ "worm", nil], @ "animal ",
20 [nsarray arraywithobjects: @ "Flower", @ "Grass", @ "sunflower", nil], @ "plant ",
21 [nsarray arraywithobjects: @ "crazy rock", @ "granite", @ "pebbles", nil], @ "Stone", nil];
22
23 subpickerarray = [dicpicker objectforkey: @ "animal"];
24 textfield. inputview = selectpicker;
25 textfield. inputaccessoryview = donatelbar;
26 textfield. Delegate = self;
27 selectpicker. Delegate = self;
28 selectpicker. datasource = self;
29 selectpicker. Frame = cgrectmake (0,480,320,216 );
30
31}
Assign a value to nsdictionary * dicpicker; and add the corresponding array to the three keywords.
3. Component returns two gears.
[CPP] view plaincopy
32-(nsinteger) numberofcomponentsinpickerview :( uipickerview *) pickerview {
33 return 2;
34}
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.
[CPP] view plaincopy
35-(nsinteger) pickerview :( uipickerview *) pickerview numberofrowsincomponent :( nsinteger) component {
36 IF (Component = kfirstcomponent ){
37 return [pickerarray count];
38} else {
39 return [subpickerarray count];
40}
41
42}
6. Return corresponding String Data Based on Component
[CPP] view plaincopy
43-(nsstring *) pickerview :( uipickerview *) pickerview titleforrow :( nsinteger) Row forcomponent :( nsinteger) component {
44 If (Component = kfirstcomponent ){
45 return [pickerarray objectatindex: Row];
46} else {
47 return [subpickerarray objectatindex: Row];
48}
49}
7. When the gear on the left is dragged, the data on the right is reloaded accordingly.
[CPP] view plaincopy
50-(void) pickerview :( uipickerview *) pickerview didselectrow :( nsinteger) Row incomponent :( nsinteger) component {
51 if (Component = kfirstcomponent ){
52 subpickerarray = [dicpicker objectforkey: [pickerarray objectatindex: Row];
53 [pickerview selectrow: 0 incomponent: ksubcomponent animated: Yes];
54 [pickerview reloadcomponent: ksubcomponent];
55}
56}
8. The selected data is displayed on textfield.
[CPP] view plaincopy
57-(void) textfielddidendediting :( uitextfield *) textfield {
58 nsinteger firstviewrow = [selectpicker selectedrowincomponent: kfirstcomponent];
59 nsinteger subviewrow = [selectpicker selectedrowincomponent: ksubcomponent];
60 nsstring * firststring = [pickerarray objectatindex: firstviewrow];
61 nsstring * substring = [[dicpicker objectforkey: [pickerarray objectatindex: firstviewrow] objectatindex: subviewrow];
62 nsstring * textstring = [[nsstring alloc] initwithformat: @ "you selected: % @", firststring, @ "", substring];
63 self. textfield. Text = textstring;
64}
65
66-(ibaction) selectbutton :( ID) sender {
67 [textfield endediting: Yes];
68}
Success! Run. Click textfield to bring up: