0

I am trying to make a cocoa app with an NSplitView that keeps track of "active subviews" (like how the Terminal app knows which pane you are editing).

I have a SessionWindowController that keeps track of the "currentPaneContainerViewController" based on which PaneView the user last clicked on.

some classes and files:

SessionWindowController.h/.m 
PaneContainerViewController.h/.m
PaneView.h/.m 
PaneContainerView.xib

PaneContainerView.xib has the PaneContainerViewController as its file owner.

My current implementation is as follows:

To access the PaneContainerViewController from the NSView, I use IBOutlets that reference to the file owner, and to access the SessionWindowController I also maintain an IBOutlet of the object conforming to a delegate method I made (namely, the object happens to be the SessionWindowController).

#import <Cocoa/Cocoa.h>
#import "PaneViewDelegate.h"
@class PaneContainerViewController;
@class SessionWindowController;

@interface PaneView : NSView
{
    //outlet connection to own controller
    IBOutlet PaneContainerViewController *myPaneContainerViewController;

    //we'll use delegation to get access to the SessionWindowController
    IBOutlet id<PaneViewDelegate> sessionDelegate;
}
@implementation PaneView

-(void)mouseUp:(NSEvent *)event
{
    if (sessionDelegate && [sessionDelegate respondsToSelector:@selector(setCurrentPaneContainerViewController:)]) {
        [sessionDelegate setCurrentPaneContainerViewController:myPaneContainerViewController];
    }
}

Here is the class that sessionDelegate belongs to, which conforms to the PaneViewDelegate protocol:

@interface SessionWindowController : NSWindowController <PaneViewDelegate>
{
    PaneContainerViewController *currentPaneContainerController;
}
- (void)setCurrentPaneContainerViewController:(PaneContainerViewController*)controller;

My trouble is accessing the SessionWindowController object using the IBOutlet. In Interface Builder, what do I connect the sessionDelegate outlet to to be able to access the SessionWindowController instance? Furthermore, is it okay to pass the controller to the delegate instead of an NSEvent?

I am new to Cocoa, so if there is a better design pattern do please let me know. This does seem like a lot of boilerplate for a pretty common functionality.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
ejang
  • 3,982
  • 8
  • 44
  • 70
  • “… is it okay to pass the controller to the delegate …?” Which controller? The SessionWindowController *is* the delegate and it owns the PaneContainerViewController. – Peter Hosey Jun 11 '13 at 15:53
  • Sorry, my question wasn't phrased well. I want to access the SessionWindowController delegate from the PaneContainerViewController, but I don't know how to hook up the delegate to the PaneView. – ejang Jun 11 '13 at 19:40
  • The PaneView is the VC's view, right? So the VC can simply ask itself for its view, then get the view's delegate. – Peter Hosey Jun 11 '13 at 19:42
  • don't I have to somehow connect sessionDelegate to PaneView within Interface Builder? – ejang Jun 11 '13 at 19:45
  • It doesn't have to be connected within a nib; outlets make that possible, but they don't foreclose other ways of setting a delegate. That said, I just looked at your delegate protocol and it seems like maybe a delegate protocol isn't the right way to do this. Am I right in interpreting the “PaneContainerViewController” as referring to a VC that owns one of the views *inside* the PaneView? So, the PaneView owns one or more PCVCs? – Peter Hosey Jun 11 '13 at 19:58
  • thx for ur patience. I have PaneContainerView.xib that contains a PaneView+other buttons. The PCVC is the file's owner. an event triggers when I click on a PaneView (mouseUp in PaneView.h). Within this IBAction, I can access the PCVC that owns the PaneView via IBOutlet.I also need to access SessionWindowController that owns the PCVC.I followed example from http://stackoverflow.com/questions/6891030/how-does-an-nsview-subclass-communicate-with-the-controller but my view is created via IB so I don't know how to call setDelegate for a controller that doesn't own the view. – ejang Jun 11 '13 at 23:45

0 Answers0