Search code examples
iphoneiosuipickerview

How to use more complex datasource with UIPickerView


How to use complex datasource with UIPickerView? I've got an array of objects and want to use MyObject.Name as caption of element in UIPickerView, but get the whole object back when it's selected.

it's an NSMutableArray of objects.

`

@interface Account : NSObject
{
    NSString *ID;
    NSString *Name;
    NSString *AccountTypeName;
    NSString *Type;
}

Array looks like this:

`2012-05-15 16:30:05.847 App[2320:f803] (
    "<Account: 0x6888d70>",
    "<Account: 0x6eb09a0>",
    "<Account: 0x6d7f170>",
    "<Account: 0x6d79ca0>"
)`

I`m getting a "unrecognized selector sent to instance 0x6888d70'" error when i use it


Solution

  • You need to store all your Object in an array and in the function :

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    

    you find by name (with a NSPredicate by example) the selected item

    OR :

    [ARRAYNAME objectAtIndex:row]