4

I am in the process of building a Cocoa app, which is comprised of a window divided in 3 sections. Each section is responsible for its own business and there are around 30 controls in it between table views, pop up buttons etc.
I started with a single Controller but things get messy pretty easily, so I decided to break the logic down in 3 controllers object (one each section of the view). I then created the NSObject reference on Interface Builder and hooked up all the outlets, actions, data sources and delegates. So far so good.

Now, the three sections pass objects to each other and therefore I need a way to set an object from one class to another. The object in question is a class variable, but as I have no reference to the object I don't know how to pass it around.

Is there a way to do this or is this just the wrong approach overall?

Solution:

As Sergio mentioned below in one of the comments, the solution seems to be to create a weak reference to the other controllers inside each controller as IBOutlet and then in the Xcode Interface Builder link the controller objects together. As a result, now each controller can access the exposed methods and variables of the referenced controllers.

cescofry
  • 3,706
  • 3
  • 26
  • 22

1 Answers1

2

Now, the three sections pass objects to each other and therefor I need a way to set an object from one class to another. The object in question is a class variable, but as I have no reference to the object I don't know how to pass it around.

What seems missing in your design is a Model (as in Model-View-Controller). This would be a class encapsulating all the state of your app, even if it is transitory state, so that each affected object have access to it.

One easy implementation for such a model class is a singleton, so that it is readily available in all of your controllers. Have a look here for some thought about the implementation of a singleton in Objective-C.

Once you have your model class, your controllers could access it like this, e.g.:

[MyModel sharedModel].myObject = ...;

This approach is good, IMO, if it makes sense for you to go in the direction of creating a Model for your design. This depends on the semantics of the object that your controllers share. So, there might be alternative solutions better fit for your case. E.g., one controller could be the owner of the shared object, and the other two could receive a reference to the first controller on init so that they can access its public properties.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • I have a Model, which actually is a singleton, but it is the nib file which holds the references to the 2 controllers so I would not know how instantiate (or reference) them inside the model. That I guess is the part I am missing. If I can get an hang on the 2 controllers somewhere then I can easely set some delegate between each other to pass those objects around. Any Idea? – cescofry Jan 08 '13 at 19:07
  • 1
    should not be the two controllers who references the model? About the nib thing, what about defining two outlets for your nib owner and connect them to the 2 controller in IB? – sergio Jan 08 '13 at 19:09
  • There it is. This worked like a charm. Would you like to tide it up as an answer so I can accept it. – cescofry Jan 08 '13 at 20:16