0

In my interface I need to provide two NSTableViews in the same window that are structurally the same, but present different data to the user. I'm using storyboards and to do this, I've placed two Container Views in the view controller of the window. I then added a single view controller to the storyboard (outside of the window's view controller) and into that controller placed an NSTableView.

A couple of storyboard screenshots to illustrate:

enter image description here enter image description here

The NSTableView is inside the Lineup View Controller, which has its own interface and implementation. The two Lineup View Controllers are binded to the container views within viewDidLoad of the Game Board View Controller like so (showing the code for only one of the attachments):

    homeLineupView = [self.storyboard instantiateControllerWithIdentifier:@"Lineup View"];
    [self addChildViewController: homeLineupView];
    [HomeLineupContainerView addSubview:homeLineupView.view];

I'm able to successfully populate the two table views using notifications to provide the data that is unique to each table. The table views are functioning independently, as expected, with no problems.

Where I'm kind of stuck is how I now go about talking to the two table views from the view controller that manages everything else in the window (Game Board View Controller in the second figure). For example, how would I go about telling the table view inside Home Lineup Container View to select row 3? I have references to the Lineup View Controllers, but not the NSTableViews — unless there is a straightforward way to get those so I can call the methods directly. Or... do I need to set up notifications (or some other mechanism) for doing things like selecting a row, detecting a double click, etc?

Thanks!!

Adding a little more information about where I am confused

When I step through my code in the debugger I can "see" the table view inside the NSViewController that references the enclosing container view (see below).

enter image description here

You can also see my very incorrect attempt at using tableView directly to selectRowIndexes. 'visitorLineupView' is defined like so:

@interface GameBoardViewController : NSViewController <NSOpenSavePanelDelegate> {
        :
        :
    NSViewController                *visitorLineupView;
    NSViewController                *homeLineupView;
        :
        :
}

And the tableview itself is defined like so:

@interface LineupViewController : NSViewController <NSTableViewDataSource> {
        :
        :
    IBOutlet NSTableView    *tableView;
        :
        :
}

So, the procedural C programmer in me sees all this and wants to grab that 'tableView' pointer and use it directly to send it 'selectRowIndexes' message. But I know that conceptual rules of MVC are throwing up roadblocks I'm having a difficult time scaling.

johnpurlia
  • 113
  • 6
  • Search for `NSTableView`. You'll find lots of examples. – El Tomato Aug 23 '19 at 22:37
  • Thanks for responding. Yes, I've done quite a bit of searching, both here on stackoverflow, and elsewhere across the net, but have not yet found information that has helped (which is why, of course, I've posted the question). – johnpurlia Aug 23 '19 at 22:43
  • See [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Willeke Aug 24 '19 at 14:05
  • Thanks, the link is very helpful in describing data sharing between views. I'm currently using prepareForSegue and notifications to pass data back and forth between the two view controllers. and that's working great. Where I'm blocked is in calling the methods of NSTableView from the other view controller (i.e. selectRowIndexes). It's quite possible (and likely) that this is a mental block brought about by 40 years of straight C and assembly language programming where I could call anything at any time — so long as I knew what I was doing (ahem). This is my first application in objective-C. – johnpurlia Aug 24 '19 at 17:45
  • I worked around the issue (though not in a way that makes me feel particularly comfortable) by making the NSTableView in the view controller public. It works, but... ick. – johnpurlia Aug 25 '19 at 06:12
  • See the Lineup View Controller as a black box and talk to the View Controller intead of the table view. Add properties and/or methods to the View Controller to change the attributes of the table view so you can do `homeLineupViewController.data = lineups`, `homeLineupViewController.selectedLineups = lineupsToSelect` or `homeLineupViewController.selectLineups(lineupsToSelect)`. – Willeke Aug 25 '19 at 08:59
  • Thank you! This was a big help and got me past a conceptual hurdle. – johnpurlia Aug 29 '19 at 20:22

0 Answers0