Use NSWindow or NSViewController?

579 views Asked by At

If I just have a window with elements that are not removed/added, should I process all the events and other things in NSWindow, or should I create NSViewController?

2

There are 2 answers

2
ridiculous_fish On BEST ANSWER

In general you should try to handle events at the deepest location possible. For example, if your window has an NSButton, then that button should take responsibility for handling clicks on itself - not the window! Of course NSButton has APIs for this, such as setAction:.

There are some views whose contents are opaque to Cocoa, where you wish to take responsibility for event dispatch. An example is an NSOpenGLView, where you render some scene and need to direct events to components in the scene. The advice still applies: override event handlers on the NSOpenGLView (mouseDown:, etc.) and dispatch from there.

There are higher level methods available, such as [NSWindow sendEvent]. It's appropriate to override these when you wish to intervene in event dispatch for the entire window, such as redirect events to other windows. In general it's not necessary, and makes your app more fragile.

NSViewController plays a diminished role on the desktop (compared to UIKit) and is rarely necessary.

2
Raviprakash On

You can use NSWindowController or NSViewController depending on the need. If you are customising the window behaviour then only use NSWindow.