How to handle cocoa communication between multiple controller classes

1k views Asked by At

i'm new to Cocoa, maybe these questions are very basic. Currently I'm developing a mac application that uses an NSTabView, where you can switch between "app", "options" and "statistics".

Now my approach is to create three controller classes, an AppControler, an OptionsController and a StatisticsController. I created objects for them in the .xib file and linked the corresponding UI elements to the correct controller objects.

My questions are: - Is that the right way, having multiple controller objects for an NSTabView? - When I want the AppController to get information about the options, how can I do communication between App- and OptionsController?

Thanks!

3

There are 3 answers

2
Carter On BEST ANSWER

I struggled with these types of questions a lot when I started Cocoa development. I eventually found that there's no real answer. In Cocoa and Objective-C there are so many ways to skin a cat that in the end it really ends up being up to you.

Since all of your controllers are going to get loaded with the Nib, the easiest way to do it is to create an outlet in each controller for the other controller objects with which it wishes to communicate. Hookup the outlets in interface builder and you are set to go.

This is an easy and effective solution in my mind. You are going to end up with circular references between the controllers but Objective-C's Nib loading/unloading code is going to take care of all of that for you.

0
lottscarson On

For tab views, I usually just make one controller for all of the tabs in that window, though I'd probably split it up if was using more than 3 or 4 tabs, and each tab required a significant amount of code to handle the actions and outlets.

0
EricLeaf On

Typically, an MVC paradigm is used. In your case, if one controller is creating or modifying some setting you set it in the model in some way (could be as simple as some user defaults set), and if thats used elsewhere you read it from the model.