PDA

View Full Version : PickerView




rustywild
Apr 2, 2011, 07:18 AM
Hi:

I have a pickerview with two dependent component that use a dictionary. All work fine but i want show two result in two label according with your selecction in the picker.

How i can do that??


thanx



Sykte
Apr 2, 2011, 09:16 AM
Hi:

I have a pickerview with two dependent component that use a dictionary. All work fine but i want show two result in two label according with your selecction in the picker.

How i can do that??


thanx

Not 100% sure what you're looking for. Are you looking to extract the values from the picker or overlay the labels.

rustywild
Apr 2, 2011, 11:06 AM
Not 100% sure what you're looking for. Are you looking to extract the values from the picker or overlay the labels.

I want that:
if value in the first component is cars, the second component is mustang the fisrst label must show "1970" and the second label must show "v8"

thanx

xStep
Apr 2, 2011, 02:31 PM
I want that:
if value in the first component is cars, the second component is mustang the fisrst label must show "1970" and the second label must show "v8"

You're just not making a lot of sense to me either. It sounds like you are saying you have four components; vehicles, model, year, engine. Maybe your example mistakenly leads me to that conclusion. Your first post clearly says you have two components.

If by labels you mean the value that is selectable, than please refer to that as a component title, as that corresponds to the method name;
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

You need to clearly express in much more detail what it is you are looking at doing. Brevity is your enemy when asking programming questions.

rustywild
Apr 3, 2011, 05:10 AM
i have only two components that use a dictionary in the picker, only two and the labels have to show a result that is not in the components



thanx again

rustywild
Apr 3, 2011, 02:55 PM
if it helps, i can write the code

xStep
Apr 3, 2011, 04:01 PM
As I said, you need to be explicit about what it is you have and the result you want. For instance...

I have a dictionary that I'd like to use with a UIPickerView to select two values from and display in two UILabels in my app. The dictionary key is a string to be displayed in the first component of the picker. The dictionary value for each key is an array of strings. When the left component changes, I'd like the second component to reflect the appropriate values for the key. Finally, I'd like to use the selected values with my labels.

And yes, current code sample can help and is often asked for because it can make clearer what problem a person is having. When you supply code, wrap it in the CODE tags.

xStep
Apr 4, 2011, 07:12 PM
rustywild sent me a private message and this is my response to that.


I only slept 4 hours last night so I might still be misunderstanding. Part of your explanation didn't make any sense given your code.

This is what I believe you have…

A dictionary where the keys will be the left most component that sets up what the right side component should display. The keys are placed into a sorted array. Each time component 0 changes, you want to update the right side component. Each time a choice is made, you want it reflected in two labels. The following code should do that for you. This is untested code.


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == kPotenciaComponent) {
NSString *selectedState = [self.potencia objectAtIndex:row];

// Update values array for key of component 0.
[self.cantidad release];
self.cantidad = [potenciavoltaje objectForKey:selectedState];

[pickerView reloadAllComponents]; // Might not need to load all, but I do.
[pickerView selectRow: row inComponent: kPotenciaComponent animated: YES];
[pickerView selectRow: 0 inComponent: kCantidadComponent animated: YES];
label1.text = [self.potencia objectAtIndex:row];
label2.text = [self.cantidad objectAtIndex: 0];
}
else {
label2.text = [self.cantidad objectAtIndex: row];
}
}


The labels are updated here because that is when the change is occurring in your selection. You may also want to set them when you first setup your two data arrays in your viewDidLoad method.

P.S. Looks like you are a native Spanish speaker. Welcome to the challenge of communicating technical problems in another language. :)