Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The difference between iOS and OS X is that there's only one window in iOS but there can be multiple in OS X. <code>NSViewControllers</code> in OS X work differently than <code>UIViewControllers</code>in that the <code>NSViewController</code> is designed to load and handle a single view, while <code>UIViewControllers</code> (apologies for my relative lack of iOS knowledge) seem to handle multiple views and provide much more of the glue between views. </p> <p>Even the <a href="http://developer.apple.com/library/mac/#documentation/General/Conceptual/MOSXAppProgrammingGuide/CoreAppDesign/CoreAppDesign.html">Apple docs</a> write that: </p> <blockquote> <p>In Mac OS X, AppKit view controllers are assistants to the window controller, which is ultimately responsible for everything that goes in the window.</p> </blockquote> <p>Hence, the counterpart for <code>UIViewController</code> in OS X isn't <code>NSViewController</code>, but rather <code>NSWindowController</code>, which does provide for OS X much of what <code>UIViewController</code> does for iOS by managing the entirety of an individual window and the layout/content/interaction of the views within.</p> <p>In your case, I would use an <code>NSWindowController</code> - though if the app is very simple, the <code>App Delegate</code> works too; and if the app is very complex, then using a <code>NSViewController</code> to split up the code wouldn't be a bad idea. </p> <p>The best way to do use an <code>NSWindowController</code> would be to programatically load it in the App Delegate using <code>[[CustomWindowController alloc] init]</code> and </p> <pre><code>@implementation CustomWindowController - (id)init { self=[super initWithWindowNibName:@"CustomWindowNibName"]; if(self) { //perform any initializations } return self; } @end </code></pre> <p>Or calling </p> <pre><code>[[CustomWindowController alloc] initWithWindowNibName:@"CustomWindowNibName"]; </code></pre> <p>directly (and overriding initWithWindowNibName) if you want it to be reusable.</p> <p>And you can delete the default window in MainMenu.xib.</p> <hr> <p>Fundamentally, more often than not, an NSWindowController manages a window instantiated in its own nib file. The NSWindowController usually owns that nib file. (Though it is possible to have it manage a programmatically created window, that isn't usually how it's done.)</p> <p>To be able to use a custom <code>NSWindowController</code>, you therefore need to make your window-to-be-managed in a separate nib/xib file. (using the default xib file means that you allow Cocoa to automatically instantiate a NSWindowController without opportunity for subclassing; you cannot use a custom <code>NSWindowController</code> with the default NSMainNibFile. For a simple app, just put all the controller code in the NSApplication/App Delegate)</p> <p>In Xcode 4 at least, the process involves creating a xib with the window template and the custom <code>NSWindowController</code> class, instantiating the <code>CustomWindowController</code> class based on that nib in -init or wherever and then calling <code>[CustomWindowController showWindow:self];</code> (or if that doesn't work, <code>-makeKeyAndOrderFront:</code>) to get the window to actually show (in <code>- (void)applicationDid/WillFinishLaunching:(NSNotification *)aNotification</code> might be a nice place).</p> <p>To stop the default window from showing I just delete it. There's probably a better way but I don't know that.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COYeah I figured I needed to use an NSWindowController but I wasn't sure how to hook it up to the NSWindow. I dragged a new window into the MainMenu.xib and set it up but it is already being loaded when I run the app. I also tried using the AppDelegate just to get things started but I don't know how to eg. make my NSTableView look in AppDelegate.m for the NSTableViewDataSource prototype methods. I tried connecting from the table to 'File's owner' and selected dataSource but it fails with an error 'invalid NSTable view data source'.
      singulars
    2. COThe easiest way to hook up the NSWindowController is to split up your nib files and give it its own file to load. So make a new nib file using the window template and set the Files Owner to the controller. Also, about your attempt to connect to the dataSource: 'File's Owner' in that case was the NSApplication; you want to drag to the App Delegate instead.
      singulars
    3. COOk so I made a new controller 'MainWindowController' and ticked 'with xib file'. I copied the NSwindow from the MainMenu.xip and pasted it in the new MainWindowController.xib file deleting the old one. The 'Custom Class' of File's Owner is set to MainWindowController. I added custom init method to MainWindowController.m with `self=[super initWithWindowNibName:@"MainWindowController"];` . I instantiate my MainWindowController property in the app delegate `applicationDidFinishLaunching` method with `mainWindow = [[MainWindowController alloc] init];` .. no errors but no window. Anything I missed?
      singulars
 

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