Search code examples
iphonecocoa-design-patterns

synchronize one object between two controllers in Cocoa


I have a MainController, it creates an object(say polygon), a controller(say,polygonViewController). PolygonViewController has two outlets:

IBOutlet Polygon* aPolygon;
IBOutlet UILabel* numOfSidesLabel;

it mediates Polygon and PolygonViewController.xib file.

How do I make sure the PolygonViewController's aPolygon is the same(instance) as polygon created in mainController?

I tried to add @property Polygon* aPolygon; to PolygonViewController then in MainController I did polygonViewController.aPolygon = polygon; However, aPolygon in PolygonViewController awakeFromNib method is always null.


Solution

  • Have you tried to use a singleton object?

    You can make your aPolygon class to be a singleton object so, you will be always working with the same object in as many viewControllers as you want to use it.

    Take a look at: http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32

    Hope this will help you!

    Cheers,
    VFN