Search code examples
xcodeviewcontroller

How to access an Objective C variable from different view controllers?


I need to call:

 @property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;

from viewcontroller x but be able to access and set the button colors from view controller y. Basically I'm making a settings page that allows different color schemes. Any ideas? Thanks!


Solution

  • You can use delegation.

    Basically, viewcontroller y would be a delegate of viewcontroller x, and every time someone changes the settings page, you viewcontroller x would notify viewcontroller y of that change. X would notify Y like so:

    [delegate doSomething withParameter: parameter]
    

    Viewcontroller y would then perform certain methods with that parameter (the variable you're trying to pass).

    There are a couple of other things involved, so you should read up on delegation