views:

2012

answers:

2

Hi all!

I have an UIPickerView with 3 components populated with 2 NSMutableArrays (2 components have the same array).

A tutorial says:

//PickerViewController.m
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

NSLog(@"Selected Color: %@. Index of selected color: %i", [arrayColors objectAtIndex:row], row);
}

But I want to show the selected row for each component in an UIAlertView after the user touched an UIButton.

Is there a way to do this? Or must I just use 3 invisible UILabels as buffer?

Thanks in advance.

+2  A: 

So in your button action method, you can do something like this:

- (IBAction) showAlert {
  NSUInteger numComponents = [[myPickerView datasource] numberOfComponentsInPickerView:myPickerView];

  NSMutableString * text = [NSMutableString string];
  for(NSUInteger i = 0; i < numComponents; ++i) {
    NSUInteger selectedRow = [myPickerView selectedRowInComponent:i];
    NSString * title = [[myPickerView delegate] pickerView:myPickerView titleForRow:selectedRow inComponent:i];
    [text appendFormat:@"Selected item \"%@\" in component %lu\n", title, i];
  }

  NSLog(@"%@", text);
}

This would be the absolute formal way to retrieve information (by using the proper datasource and delegate methods), but it might be easier (depending on your set up), to just grab the selected row and then pull the information out of your model directly, instead of going through the delegate method.

Dave DeLong
Thanks! selectedRowInComponent is just what I needed.
Time Machine
A: 
//Delegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    selection=[[NSString alloc] initWithFormat:@"You have selected: %@.", [theArray objectAtIndex:row]];
}   

//My Actions
-(IBAction)selectingCat{
    [txtSelected setText: selection];
    [selection release];
}

I find that this works for me on the delegate you give a previously NSString variable established in your header file the value that you want your button to use and then on your button all you have to do is assign that value to your textfield or label or whatever and release the NSString.

Hope it helps

Rob Contreras