Difference between NSWindowController Vs NSViewController
Asked Answered
P

3

33

I am coming from iOS background and starting to learn Cocoa. On iOS unless we have multiple targets for iPad and iPhone we usually have one Window and manage the screen using UIViewControllers. Where every new screen will most of the time will map to a UIViewController.

However on cocoa this seems to be the otherway around where a new screen/window is manage by NSWindow and it's subcomponents are managed by NSViewController. So if I have multiple window app I should have separate NSWindowController for each window.

Is this approach correct or am I having a misunderstanding ?

Poltroonery answered 16/6, 2012 at 4:55 Comment(5)
What you haven't said here (and should make clear) is that you're coming from iOS and now starting to work with programming on MacOS, correct?Tijerina
You have it a bit backwards on the iOS part. The UIViewController doesn't manage the UIWindow - the UIWindow manages the UIViewController. Although there is usually only one UIViewController on-screen at a time (this has changed in iOS5 with child view controllers), they are still subcomponents of the UIWindow. Case in point, a UIWindow has a rootViewController property, but viewcontrollers don't have a window property. Now, with OSX, you can have multiple NSWindows, but they still manage their respective NSViewControllers. In short, hierarchy is: Window > ViewController > ViewsSewn
Thanks , Yeah I am coming from a iOS background :) Lets say I have multiple separate windows on my mac application. What's the bets approach to take , where each window will map to a windowController or to viewController . For example when we double tap a file in xCode it opens up a new window.Poltroonery
I don't understand why you need separate UIWindows for each target in iOS. I use the same window for iPhone/iPad (it is easy to set automatically to screen bounds)Shrewd
Even on an automatically created xcode project there are two MainWindows for each target , correct me if I am wrong ?Poltroonery
C
15

With iPhone SDK and Leopard SDK, they introduced view controllers, or NSViewController and UIViewController. As their names suggest what they do is to manage views

The view controllers are for managing views. Current trend in UI design is Single Window, Multiple View. What it means is that there is one Window and inside of it, different group of views designed for different purpose can be swapped in and out. So, the View Controllers handles these for programmers for well-established pattern. Currently view controllers are very important for iPhone and iPod touch programming, because the platform is based on Single-Window and Multiple View model. However, it doesn’t seem to me that using view controller is very popular for Mac. how about the window controller like NSWindowController? Its counterpart, UIWindowController doesn’t exist for the iPhone and iPod touch environment, because there is only one window for those environment. Unlike view controllers, the NSWindowController is for document based programs. Well, document based program can use multiple window. So, it is reasonable to think that NSWindowController is for document based programs as Apple’s document says.

Cowshed answered 16/6, 2012 at 5:49 Comment(2)
On I iPhone development we use UIVIewControllers extensively typically a new screen will managed by a UIViewController. However on macs is it safe to assume this is done through the NSWindowController ? Reading on Apple Documentation they seems to assume that all the multi window apps belongs to document based apps. So if we are developing an multi window app like xcode , is that has to document based as well ? Even IOS has UIWindowController as I know it's not in published API.Poltroonery
You can have multiple windows without document-based apps. For example, persistent panels/inspectors...Shrewd
S
5

I also come from iOS and started coding Mac apps a while ago, learning mostly from Apple's documentation.

My impression is that on the desktop, you almost never need NSViewControllers (one big exception would be a window with tabs and multiple views, like the GarageBand welcome screen).

Most of the time you have one NSWindowController per window. Learn first the relation between NSWindow and NSWindowController (and NSDocument, if you are making a document-based app).

Once you got that right, start experimenting with NSViewController.


UPDATE: It seems that since the introduction of storyboards for mac apps too, Apple expects that most of the view presentation logic should be migrated from the older NSWindowController to the newer NSViewController, more in line with how an iOS app is structured. I am not very knowledgeable on exactly where to draw the line, or what kind of code should remain in the window controller (or whether it still needs to be subclassed at all).

Shrewd answered 16/6, 2012 at 7:5 Comment(1)
So the impression I got so far is since you can't have multiple windows on iOS, the model is multiple view controllers in single window though we get the impression we work with different screena. A screen is depicted by a Viewcontroller on iOS. But on Mac OSX since you can have multiple windows basically each window is managed by a window controller and we can include view controllers to manage the sub components.Poltroonery
C
2

A Window Controller creates a traditional window and has all the traditional APIs.

A View Controller can also be shown outside of another view. It then gets a window frame but does not support the full traditional Window Controller API.

In addition to custom, modal, and show, a View Controller can also be presented in the modes sheet and popover.

So, View Controller has more presentation options and a more streamlined API but probably a few limitations in cases that are covered by the traditional Window Controller.

Conciliate answered 14/10, 2020 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.