2

I would like to develop an application where the main Window would have a NSTabView with several NSTabViewItems, and in each item it will have a different NSTableView.

How would you organize the class architecture in order to be able to access from each tab tableview controller to the others? What is the best way to do it?

I have read a lot of stuff about passing data between controllers using delegates and so on, but I think there must be another clearer way to do address this problem.

Having the next example scenario:

TabOneController.h

@interface TabOneController : NSControl {
    NSMutableArray *listOne;
        ...
}

TabTwoController.h

@interface TabTwoController : NSControl {
    NSMutableArray *listTwo;
        ...
}

These two controllers implements NSTableView dataSource and delegate methods using its NSMutableArray list for it. I would like a proper scenario where both controllers could have access to each other (class methods, View Controller, another controller managing all or whatever a good idea would fit here). What do you think it is the best way for it?

Thanks in advance

Community
  • 1
  • 1
GoRoS
  • 5,183
  • 2
  • 43
  • 66

3 Answers3

2

If you want to share data between controllers, a good solution is to pass all controllers the same instance of the data object. If there’s more data to be shared or if there’s some extra functionality attached to the data, wrap it in a model class and let the controllers share a pointer to the same model.

If you want the controllers to call each other, there are many possibilities with different implications and there is no definitive answer, everything depends on the situation. The important question is: why would all the tab controllers want to access other tab controllers? Generally you should keep the controllers isolated. This is called loose coupling and it’s very good for your design.

If the controllers need to call each other, try to rethink the design. Maybe some of the behaviour should go to the model instead? For example, instead of calling another controller to delete an item from a list, you can move the deletion code to the model, and other interested controllers can learn about the model changes by observing the model.

Whatever you do, just don’t use singletons :) I have put some sample code on GitHub that shows how to wire a project without (mis)using singletons.

zoul
  • 102,279
  • 44
  • 260
  • 354
1

First option: Declare instance variables for each controller that point to other controllers? For example, say you created a property anotherController in CustomController1 and:

CustomController1 *controller1 = [[CustomController1 alloc] init];
CustomController2 *controller2 = [[CustomController2 alloc] init];
controller1.anotherController = controller2;

This way, controller1 will have access to controller2. And then just do similar stuff to other controllers. You can even apply some inheritance there.

Second option: From a particular controller do:

CustomController *customController =
(CustomController *)[self.tabBarController.viewControllers objectAtIndex:x]

Assuming CustomController is the class of the controller you want to access, and x is where it is located in the array that is maintained by the tab bar controller.

tux91
  • 1,674
  • 10
  • 16
0

I would create a singleton class that will store data and handle all data operations... You can store the data in the NSUserDefaults as the simplest practice.

If you are only going to be working with a few shared variables, I would just use the AppDelegate.

Lefteris
  • 14,550
  • 2
  • 56
  • 95