如何切换 Cocoa 应用程序中的视图?
所以我开始学习如何使用Cocoa。我想我已经基本明白了,但我对创建和切换视图很感兴趣。我正在重写我之前制作的一个游戏以供练习。我想要的只是一个窗口(最好是不可调整大小),并且我希望能够在游戏中的不同屏幕上切换视图。
首先,我有主菜单(开始游戏、高分、退出)。然后我需要每个屏幕(游戏屏幕、高分屏幕)一个窗口。
我感到困惑的是如何设计这个。我查了一下 NSViewController 认为它管理视图,但事实并非如此。它只通过真正加载来管理一个视图。我不明白为什么我需要使用 NSViewController 那么。难道我不能只拥有一个包含 NSView 的多个子类的窗口类并像这样加载它们吗?我不确定我是否理解 ViewController 的用途。
我的窗口类真的需要子类化 NSWindowController 吗?我试图遵循 Apple 的 ViewController 示例,它有一个窗口控制器类,它是 NSWindowController 的子类。我不明白对其进行子类化的目的是什么。所有 NSWindowController 似乎添加的是 - initWithPath:(NSString *)newPath
但当我可以编辑 plist 文件以在启动时打开窗口时,我看不到其中的用途。 Apple 的示例还有一个 NSView 变量和一个 NSViewController 变量。难道你只需要一个变量来存储当前视图吗?
预先感谢大家,我真的很困惑这是如何工作的。
So I'm beginning to learn how to use Cocoa. I think I've pretty much got it but I'm hung up on creating and switching views. I'm rewriting a game I made a little bit ago for practice. All I want is one window (preferably not resizable) and I want to be able to switch out views for different screens in the game.
First, I have the main menu (Start Game, High Scores, Exit). Then I need a window for each screen (Gameplay screen, Highscore screen).
What I'm getting confused with is how to design this. I looked up NSViewController thinking it manages views but it doesn't. It only manages one view by loading it really. I don't understand why I'd need to use NSViewController then. Couldn't I just have a window class that contains multiple subclasses of NSView and load them like that? I'm not sure I understand the purpose of the ViewController.
Does my Window Class really need to subclass NSWindowController? I was trying to follow the example of Apple's ViewController example and it has a window controller class that's a subclass of NSWindowController. I don't see what the purpose was of subclassing that. All NSWindowController seems to add is - initWithPath:(NSString *)newPath
but I fail to see the use in that either when I can just edit the plist file to open the window on start up. Apple's example also has an NSView variable and an NSViewController variable. Don't you only need one variable to store the current view?
Thanks in advance guys, I'm really confused as to how this works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您正在尝试交换窗口内的内容视图?如果是这种情况,您可以使用
-[NSView ReplaceSubview:with:]
和-[NSWindow contentView]
作为接收器。假设您有一个名为
titleView
的标题页和一个名为menuView
的菜单页,并且您希望将它们调入和调出应用程序的主窗口。如果标题页可见并且用户单击“主菜单”按钮或链接,您可以在按钮的委托方法中放置类似的内容:需要注意的两件事:
titleView
在本例中,由该调用释放。如果您希望它仍然可用,则必须在替换之前保留
它。contentView
很容易,但您可以在组合中添加一点核心动画以赋予其一些样式。我希望这有帮助!
It sounds like you're trying to swap out the content view within a window? If that is the case, you can use
-[NSView replaceSubview:with:]
with-[NSWindow contentView]
as the receiver.Say you have a title page named
titleView
and a menu page namedmenuView
and you want to swap these in and out of your application's main window. If the title page is visible and the user clicks on a "main menu" button or link, you would put something like this in the button's delegate method:Two things to be aware of:
titleView
in this case, is released by this call. If you want it still be available, you will have toretain
it prior to replacing it.contentView
is easy enough, but you can add a little Core Animation into the mix to give it some style.I hope this helps!
您使用 NSWindowController 和 NSViewController 来管理窗口或视图,因为您只需要创建 NSWindow 或 NSView 的子类code> 用于新“种类”的窗口或视图。例如,如果您想使用圆形窗口,您可以子类化
NSWindow
。如果您只想在窗口中拥有控件,则可以对NSWindowController
进行子类化。这与 NSViewController 相同:通常
NSViewController 的
视图将属于某些基类,例如NSView
(或者可能是您自己的NSView< /code> 绘制自定义背景的子类)。 NSViewController 管理该视图的子视图之间的交互,使其能够充当较大应用程序中的单个单元。
可以这样想:视图绘制,它们将原始输入事件转换为更高级别的操作。控制器向视图提供信息并处理操作。
以这种方式组合可以极大地提高代码的模块化程度,使设计、编写和调试变得更加容易。
You use
NSWindowController
andNSViewController
to manage a window or a view because you should only need to create subclasses ofNSWindow
orNSView
for new "kinds" of window or view. For example, if you wanted to use a circular window, you would subclassNSWindow
. If you just want to have controls in a window, you subclassNSWindowController
.It's the same with
NSViewController
: Generally anNSViewController's
view will be of some base class such asNSView
(or perhaps your ownNSView
subclass that draws a custom background). TheNSViewController
manages the interaction among the subviews of that view, allowing it to act as a single unit within your larger application.Think of it this way: Views draw, and they turn raw input events into higher-level actions. Controllers supply information to views, and handle actions.
Composing things this way can greatly improve the modularity of your code, making it easier to design, write, and debug.
使用 UIVew 中定义的以下函数(它是现有窗口的一部分)
Use following functions defined in UIVew (which is part of your existing window)
我是可可的新手,但我认为你的应用程序并不真正需要 NSViewController。 NSViewController 在 MVC 设计模式中扮演“控制器”的角色。因此,它“控制”单个视图内的所有操作(粘合逻辑)。
例如,在具有多个控件的视图中:按钮、表格、复选框、文本字段、日期选择器等,这些控件之间可能存在需要绑定和更新的详细交互。例如,单击按钮会加载数据库获取,并进行错误处理和验证。该粘合逻辑进入 NSViewController 类。
在您的应用程序中,听起来每个视图都是一个简单的页面,几乎不需要视图级别控制。因此,您可能需要一个 NSWindowController,一个控制器来处理页面之间转换的逻辑和事件。
处理填充单个窗口的多个视图的一种技术是使用 NSTabView 并将选项卡样式设置为 IB 中的“无边框”。然后使用按钮操作选择所需的 NSTabViewItem。在开发过程中,将选项卡视图样式设置为“顶部选项卡”,IB 允许您通过选项卡浏览要显示的子视图。
I'm new to cocoa but I think your app does not really require an NSViewController. An NSViewController plays the role of "Controller" in the MVC design pattern. So it 'controls' all of the actions - the glue logic - within a single view.
For example, in a view with multiple controls: buttons, tables, checkboxes:, textfields date pickers, etc, there is likely detailed interactions between those controls that require bindings and updates. For example, a button click loads a database fetch, with error handling and validation. That glue logic goes into the NSViewController class.
In your app, it sounds like each view is a simple page with little need for view level control. So, you probably need a single NSWindowController, a controller to handle the logic and events for transitioning from page to page.
One technique for handling multiple views that fill a single window is to use a NSTabView and set the tab style to 'borderless' in IB. Then use button action to select the required NSTabViewItem. During development, set the tabview style to 'top tabs' and IB allows you to tab thru the subviews you want to display.