0

I have two UIPickerViews that need to go to different datasouces. The closest answer I could find was this: Multiple UIPickerViews

But I can't figure out how to do the follwoing:

you can create two classes - one data source for each picker view, and manually assign them to the picker view instances in the viewDidLoad method

Seems easy, but code really use an example.

Community
  • 1
  • 1

1 Answers1

0
  1. You need to create two classes for your data sources. They obviously should be derived from NSObject and implement UIPickerViewDataSource protocol.

  2. Now you should bound these classes to your UIPickerView. The easiest solution is to add instance variables for each data source to UIViewController, initialize them and assign to dataSource property of UIPickerView.

    @interface MyViewController
    {
        ...
        MyDataSource1 *dataSource1;
        MyDataSource2 *dataSource2;
    }
    
    ...
    
    @implementation MyViewController
    - (id) initWith...
    {
        ...
        dataSource1 = [[MyDataSource1 alloc] initWithSmth:smth];
        dataSource2 = [[MyDataSource2 alloc] initWithSmthElse:smthElse];
        return self;
    }
    
    - (void) viewDidLoad
    {
        ...
    
        myPickerView1Outlet.dataSource = dataSource1;
        myPickerView2Outlet.dataSource = dataSource2;
    }
    
iHunter
  • 6,205
  • 3
  • 38
  • 56