I'd like to start a Cocoa app with a ViewController
just like the iOS "Single View App" template, but there is no such template (is there a public / open source one that can be used?)
Is it true that for Cocoa apps, we don't really need one, because an NSView
can do everything already? We can just put all the event handling in our custom NSView class. Could it be that iOS requires it a lot more because rotation is handled by the ViewController and rotation is usually required? But if we use MVC, then it might be better to always use a ViewController, and if so, is there a standard way, a template, to do it?
To create a new view controller, select File->New->File and select a Cocoa Touch Class. Choose whether to create it with Swift or Objective-C and inherit from UIViewController . Don't create it with a xib (a separate Interface Builder file), as you will most likely add it to an existing storyboard.
In iOS Development, a View Controller is responsible for displaying the data of our iOS application on the screen. It acts as an interface between its Views (created by the developer) and the application data. Each ViewController in the Storyboard is assigned a Class that inherits the UIViewController class.
Create a new IBOutlet called shakeButton for your storyboard button in your ViewController. swift file. Select the shake button in Interface Builder. Then hold down the control button ( ⌃ ) and click-drag from the storyboard button into your ViewController.
A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed. Every app has at least one view controller whose content fills the main window.
The "Controller" in OS X with respect to managing NSViews is the NSWindowController
. Though Drummer says that NSViewController
isn't very useful, I must disagree - it is useful for splitting up your NSWindowController
once it gets too large, and has clear logical divisions in terms of views.
You could have one NSWindowController
, and once it gets complicated enough, the NSWindowController
could delegate tasks corresponding to specific views to subclasses of NSViewController
, and in that respect it is very useful.
In the default templates (if I remember correctly) the AppDelegate
takes the role of the window controller, though it isn't technically one. In more complex applications it is a good idea to instantiate a window controller instead.
As long as you don't mix up the controller and view anything can be used. The View should be relegated to just display and basic input handling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With