xcode 在 Mac OS X Cocoa 应用程序中添加 ViewController 的标准方法是什么?(还是必须的?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12448859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:30:28  来源:igfitidea点击:

What is a standard way to add a ViewController in a Mac OS X Cocoa app? (or is it required?)

xcodemacoscocoa

提问by Jeremy L

I'd like to start a Cocoa app with a ViewControllerjust like the iOS "Single View App" template, but there is no such template (is there a public / open source one that can be used?)

我想使用类似于ViewControlleriOS 的“单一视图应用程序”模板来启动 Cocoa 应用程序,但是没有这样的模板(是否有可以使用的公共/开源模板?)

Is it true that for Cocoa apps, we don't really need one, because an NSViewcan 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?

对于 Cocoa 应用程序,我们真的不需要一个,因为它NSView已经可以做任何事情了吗?我们可以将所有事件处理放在我们自定义的 NSView 类中。是不是因为旋转由 ViewController 处理并且通常需要旋转,所以 iOS 需要它更多?但是如果我们使用 MVC,那么总是使用 ViewController 可能会更好,如果是这样,有没有标准的方法,一个模板来做到这一点?

回答by Vervious

The "Controller" in OS X with respect to managing NSViews is the NSWindowController. Though Drummer says that NSViewControllerisn't very useful, I must disagree - it is useful for splitting up your NSWindowControlleronce it gets too large, and has clear logical divisions in terms of views.

OS X 中管理 NSView 的“控制器”是NSWindowController. 虽然 Drummer 说这NSViewController不是很有用,但我必须不同意 -NSWindowController一旦它变得太大,它就很有用,并且在视图方面具有清晰的逻辑划分。

You could have one NSWindowController, and once it gets complicated enough, the NSWindowControllercould delegate tasks corresponding to specific views to subclasses of NSViewController, and in that respect it is very useful.

您可以拥有一个NSWindowController,一旦它变得足够复杂,就NSWindowController可以将与特定视图对应的任务委托给 的子类NSViewController,在这方面它非常有用。

In the default templates (if I remember correctly) the AppDelegatetakes 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.

在默认模板中(如果我没记错的话)AppDelegate它扮演了窗口控制器的角色,尽管它在技术上不是一个。在更复杂的应用程序中,最好改为实例化窗口控制器。

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.

只要你不混淆控制器和视图,任何东西都可以使用。视图应该被降级为仅显示和基本输入处理。

回答by DrummerB

On OS X NSViewControllerisn't as often used as UIViewControlleron iOS. One of the reasons is that it's not really useful and lacks a lot of the nice features of UIViewController. There are only a couple of situations where you really have to use them, like when using an NSPopover.

在 OS XNSViewController上不像UIViewController在 iOS 上那样经常使用。原因之一是它不是真的有用,并且缺少UIViewController. 只有在几种情况下您确实必须使用它们,例如使用NSPopover.

There are several ways to structure your OS X code. One of them is using NSWindowController. You can think of NSWindowControlleras the equivalent of UIViewControlleron iOS.

有多种方法可以构建您的 OS X 代码。其中之一是使用NSWindowController. 您可以将其NSWindowController视为UIViewControlleriOS 上的等价物。