PickerView

Discussion in 'iOS Programming' started by rustywild, Apr 2, 2011.

  1. macrumors newbie

    Joined:
    Mar 9, 2011
    #1
    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
     
  2. macrumors regular

    Joined:
    Aug 26, 2010
    #2
    Not 100% sure what you're looking for. Are you looking to extract the values from the picker or overlay the labels.
     
  3. thread starter macrumors newbie

    Joined:
    Mar 9, 2011
    #3
    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
     
  4. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    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;
    Code:
    - (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.
     
  5. thread starter macrumors newbie

    Joined:
    Mar 9, 2011
    #5
    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
     
  6. thread starter macrumors newbie

    Joined:
    Mar 9, 2011
  7. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #7
    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.
     
  8. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #8
    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.

    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. :)
     

Share This Page