Search code examples
objective-cxcodeuipickerview

How to display an object created in one class in another class in Objective-C?


I have created a UIPickerView in a view controller called ActivityLevelPickerViewController. I want to get this picker view to display in a view controller called RegisterViewController.

How do I call the picker view in RegisterViewContrller?


Solution

  • There are two options. One is to just copy over all the code related to the pickerView from ActivityLevelPickerViewController to your RegisterViewController. The other option is to create a new UIPickerView subclass and move all the code related to populating the rows there. The nice thing about creating a subclass is that if you want to make a change to your picker, you can just change it once rather than in every view controller. The property declaration would look something like this:

    @property (weak, nonatomic) IBOutlet MyPickerViewSubclass *pickerView;
    

    Don't forget to import your picker view subclass in your view controller if you go with the subclassing option.