Note that there are some explanatory texts on larger screens.

plurals
  1. PONSWindowController clarification of understanding
    primarykey
    data
    text
    <p>I have used NSWindowController in projects several times, and feel like I have a (very)rough grasp of the concepts behind this important class. What I would like to do with this post is to clarify/correct my own understandings, and hopefully help other learners get that first step into understanding. It's the <strong>at-a-glance</strong> concepts, overview, and best practices that I find is most useful, and often lacking in the documentation. Here is my take on NSWindowController (questions are interspersed in bold):</p> <ul> <li>An NSWindowController (NSWC) subclass exists (conceptually) just beneath every window nib, acting as the glue between the user interface elements and the model objects that they control/represent. Basically, every window in your application should have its own NSWC subclass.</li> <li>The File's Owner of the nib should always be the NSWC subclass. <strong>Is this the case even for the MainMenu.xib application?</strong></li> <li>The NSWC <code>window</code> property should always be linked to the NSWindow in InterfaceBuilder.</li> <li>You should override the 'init' method, using <code>[super initWithWindowNibName:]</code>, so that when you refer to <code>[mycontroller window]</code> it will load the nib. <strong>Should this also be the case for the NSWC for the MainMenu.xib window, even though this is opened at startup?</strong></li> <li>The NSWC shouldn't do too much heavy lifting - it should simply pass messages to instances of objects, and present those objects in the UI.</li> <li>It can modify the UI using binding, or acting as a delegate for tables etc., or by actively changing the UI elements when it observes a change, or a combo of any of the above (which one you use seems to be a matter of taste, with pros and cons on all sides).</li> <li>An NSWC can create instances of other NSWCs when necessary (for example, when opening a one-off sub-window). </li> <li><p>Use the <code>[mycontroller showWindow:nil]</code> to display the associated window at the front. If you want the window to appear as a sheet, use something like:</p> <pre><code>NSWindowController* mycontroller = [[MyController alloc] init]; [NSApp beginSheet: [mycontroller window] modalForWindow: [self window] modalDelegate: self didEndSelector: @selector(didEndMySheet:returnCode:contextInfo:) contextInfo: nil]; </code></pre></li> </ul> <p>The <code>didEndSelector:</code> should be a method of the NSWC of the parent window, and can access and release 'mycontroller' with <code>[sheet windowController]</code>. - To close the window call the <code>performClose:</code> method of NSWC's window.</p> <p><strong>Some Questions:</strong> </p> <ul> <li>Should the NSWC of the MainMenu window also be the application delegate, or should this be a different class?</li> <li>In the same vein, should the main NSWC handle files (drag/drop and opening), or should it be passed on to the app delegate, or is that just a matter of taste?</li> </ul> <p>Please correct me if any of this is bad practice, or is just plain wrong. I am looking to clarify my understanding of NSWindowController, so any additions (in the form of best practices, experiences, gotchas) would be highly appreciated.</p> <p>Thanks, Laurie</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload