Search code examples
xcodemacoscocoanswindownsviewcontroller

NSWindow vs ViewController - OS X - Cocoa


I have been making iOS apps for a while now and I decided that I wanted to start working on making some of them for the Mac too.

The question I have is this: is there any need for an NSWindow, now that developing for the Mac is so similar to iOS??

So I made a simple cocoa application using Xcode and its comes with a storyboard called "Main", just like on iOS.

In that storyboard file, there is a NSWindow which then links to a NSViewController.

Can I get rid of that NSWindow? As I tried setting the NSViewController as the "Initial Controller" and the app still works fine. So whats the point of the NSWindow?

Also, what class links to the NSWindow? I was trying to blur that background of the NSWindow, but I have no way of linking code to the NSWindow.

Sorry for my stupid questions, but I am completely new to development for OS X.

Thanks for your time, Dan.


Solution

  • Those are many questions in one question:

    1. Can I get rid of NSwindow? No, you need a window to show you views.
    2. What is the point of the NSWindow? NSWindow is needed as the window in which the views are displayed and your events are going up the responder chain.
    3. What class is linked to NSWindow? Obviously the NSWindow class, but that is not what you want to know. I think you want to know the delegate that controls NSWindow. This is NSWindowController, although for the MainMenu.xib it is NSAppDelegate.

    Hope this gives you the answers you need. An example for working with views in a window is given in this question.

    Please, see for further details the windows programming guide, which states:

    The NSWindow class defines objects that manage and coordinate the windows an application displays on the screen. A single NSWindow object corresponds to at most one onscreen window. The two principal functions of an NSWindow object are to provide an area in which NSView objects can be placed and to accept and distribute, to the appropriate views, events the user instigates through actions with the mouse and keyboard.