Interface Builder - Share a C++ instance for action handlers?

Discussion in 'Mac Programming' started by mysterd429, Apr 20, 2008.

  1. mysterd429 macrumors newbie

    Apr 20, 2008
    Hi all,

    I have all of the actions of the different window widgets go to various Objective-C controller classes, and I'd like all of those controllers to call function in a single instance of a main controller class written in C++ for the entire window. (I'm writing a Cocoa front-end to some C++.)

    What I was thinking was to have a sub-class of NSWindow that has a pointer to a C++ class representing the overall controller for the window. Each of my Objective-C controller classes has an outlet for their respective widgets. I figured I could use the [NSView window] message for each of the outlets to get at that pointer. Is that a reasonable way of doing that? If so, when should I construct the C++ class? Is adding the awakeFromNib message to my NSWindow sub-class the right time? If so, when should I deconstruct it?

  2. Gelfin macrumors 68020


    Sep 18, 2001
    Denver, CO
    You've created a separate controller class for every widget on the window? That's not really common practice in my experience. Typically you'll have a single controller for your window with several IBOutlets and IBActions.

    After you do that there probably isn't any need for a second controller in C++. A typical pattern would be to instantiate your model in awakeFromNib and destroy it in dealloc. I can't guarantee any of that without knowing the specifics of your own project.
  3. mysterd429 thread starter macrumors newbie

    Apr 20, 2008
    Thanks for your reply!

    That makes a lot more sense than what I'm doing. I also have an NSTableView widget. If I only have one of these in the window, would I be able to use this controller as the delegate for the NSTableView?

    That pattern makes sense to me. Part of my thinking behind the C++ controller was to make it easier to port to a different platform, but I need to research that more.

Share This Page