Search code examples
objective-ciosuipickerviewuiapplicationdelegate

How change PickerView delegate from one controller to another one?


With the storyboard I've built one ViewController1 with an "Add" button. Click on add display a PopoverView (in ViewController2) with only a Picker within.

So my PickerView's delegate is in ViewController 2 but I want it in ViewController1... Is there a easy way to do this so ?

Thanks


Solution

  • In your ViewController1.h file, change your interface header to this;

    @interface ViewController1 : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>;
    

    That makes your ViewController1 in charge of the delegates. Then add the methods you want in the ViewController1.m file. Also, set the delegate of the UIPickerView to your instance of ViewController1 with this:

    [yourPickerview setDelegate:ViewController1];