我应该使用NSViewController这个吗? [英] Should I use an NSViewController for this?

查看:158
本文介绍了我应该使用NSViewController这个吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的应用程序有一个单一的文档,但几个屏幕,而不求助于几个窗口。什么是实现这一最好的方法?我想使用一个单独的NSPersistentDocument将开始一个NSViewController显示一组控件,然后交换该视图的另一个NSViewController。

I want my application to have a single document but several screens without resorting to several windows. What's the best way to achieve this? I'm thinking of using a single NSPersistentDocument that will start with a NSViewController displaying a set of controls and then swap that view for another NSViewController.

我的问题是:这是正确使用NSViewControllers?甚至可以做到吗?

My question is: is this the correct use of NSViewControllers? Can it even be done? And if the answer to both is yes, how do I swap a view for another in a document?

推荐答案

是的,它是完全的可能做到。是的,你应该使用视图控制器,但你不必。这是一个风格的选择。如果你不使用视图控制器,那么所有的控制器逻辑很可能在你的单个窗口控制器。这可能会让一个大应用程序的痛苦。 View控制器可帮助您将UI分解为逻辑组件和控制单元。

Yes, it's completely possible to do. Yes, you should use view controllers but you don't have to. That's strictly a stylistic choice. If you don't use a view controller then all the controller logic would likely be in your single window controller. That might get painful for a big app. View controllers help you break down your UI into logical components and control units.

您可以这样执行视图控制器方法:

You'd go about doing the view controller method like this:


  • 为每个组件视图创建单独的xib。

  • 为每个xib创建一个视图控制器子类,

  • 您可以进一步使用此选项,并在视图中的单独区域中使用子视图控制器。

  • 在您的窗口的awakeFromNib类中控制器,实例化所有顶级视图控制器。

  • Create separate xibs for each of your component views.
  • For each xib, create a view controller subclass that loads, owns, and controls it.
  • You could go further with this and have sub view controllers for separate areas within views.
  • In your awakeFromNib class of your window controller, instantiate all your top-level view controllers.

现在,您已经为具有多个顶级视图控制器的应用程序视图控制器。从这里,任务是在您需要它们或作为用户输入指示时在主窗口中排列视图。有时你可能有3-4顶级视图有时1,在你的情况下,你只是想换一个对另一个。这些方案之间没有巨大的区别。

Now you've got the basic framework for an app with multiple top-level view controllers. From here the task is to arrange the views in the main window as you need them or as user input dictates. Sometimes you might have 3-4 top-level views sometimes 1, and in your case you just want to swap out one for the another. There is no huge difference between these scenarios.

有很多方法可以实现这一点。

There are many ways to achieve this.

case,一个简单的方法是:

For your specific case, an easy way to go about it is:


  • 使用顶层无Tabless和无边框选项卡视图,并以编程方式设置选定的选项卡。

一般来说,您可以做一堆事情:

In general you can do a bunch of things:

    $ b $ b
  • 根据需要移动(动画)子视图进入/离开或在可视区域内。

  • 在主窗口中使用拆分视图来折叠和取消选中您想要或不想显示的区域。

  • Manually arrange the sub views in the main window's content view.
  • Move (animate) sub views in/out or within the viewable area as needed.
  • Use split views in the main window to collapse and uncollapse areas that you want or don't want showing.

BTW此方法无论您是创建基于文档的应用程序还是不是基于单窗口或多窗口文档的应用程序。

BTW this method works regardless of whether you are creating a document-based app or not or whether it is a single-window or multi-window document based app.

使用视图控制器的一个缺点是很多顶级视图布局和管理必须以编程方式完成,而不是在IB。但这不是那么困难。

The one downside to using view controllers is a lot of the top-level view layout and management has to be done programmatically and not in IB. But it's not that difficult.

通常你只需要做一个addSubview:和setFrame :,将你的视图放在你已经在IB中布局的父视图中。

Usually you just need to do a addSubview: and setFrame:, to place your views in a parent view you already laid out in IB.

但是要做更复杂和手动的东西,你应该阅读文档,并了解如下工作:框架/边界,翻转的坐标,自动调整大小,以及如何覆盖自动调整自己做自己布局。

But to do more complicated and manual stuff you should read up on the docs and get to know how the following work: frame/bounds, flipped coordinates, autoresizing, and how to override autoresizing to do your own layouts.

  • Introduction to View Programming Guide for Cocoa

这篇关于我应该使用NSViewController这个吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆