可可情节提要响应链 [英] Cocoa Storyboard Responder Chain

查看:93
本文介绍了可可情节提要响应链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于Cocoa应用程序的故事板似乎是一个不错的解决方案,因为我更喜欢您在iOS中找到的方法.但是,虽然将事情分解成单独的视图控制器在逻辑上是很有意义的,但我不清楚如何将窗口控件(工具栏按钮)或菜单交互传递给需要的视图控制器.我的应用程序委托是第一响应者,它接收菜单或工具栏操作,但是,如何访问将消息发送到的视图控制器?您可以深入查看视图控制器层次结构吗?如果是这样,您是如何从应用程序委托那里到达那里的,因为它是第一响应者?您是否可以将窗口控制器设为第一响应者.如果是这样,怎么办?在情节提要中?在哪里?

Storyboards for Cocoa apps seems like a great solution as I prefer the methodology you find in iOS. However, while breaking things up into separate view controllers makes a lot of logical sense, I'm not clear as to how to pass window control (toolbar buttons) or menu interaction down to the view controllers that care. My app delegate is the first responder and it receives the the menu or toolbar actions, however, how can I access the view controller that I need to get that message to? Can you just drill down into the view controllers hierarchy. If so, how do you get there from the app delegate since it's the first responder? Can you make the window controller the first responder instead. If so, how? In the storyboard? Where?

由于这是一个高级别的问题,所以可能无关紧要,但是,如果您想知道的话,我正在为该项目使用Swift.

Since this is a high level question it may not matter, however, I am using Swift for this project if you're wondering.

推荐答案

我不确定是否有"正确"的方法来解决此问题,但是,我想出了一个解决方案我现在将使用.首先是几个细节

I'm not sure if there is a "proper" way to solve this, however, I have come up with a solution that I'll use for now. First a couple of details

  • 我的应用程序是基于文档的应用程序,因此每个窗口都有该文档的一个实例.

  • My app is a document based application so each window has an instance of the document.

应用使用的文档可以充当第一响应者并转发我已连接的所有操作

The document the app uses can act as the first responder and forward any actions I've connected

该文档可以保留顶级窗口控制器,从那里我可以深入查看视图控制器层次结构,以找到所需的视图控制器.

The document is able to get a hold of the top level window controller and from there I am able to drill down through the view controller hierarchy to get to the view controller I need.

因此,在窗口控制器的windowDidLoad中,我这样做:

So, in my windowDidLoad on the window controller, I do this:

override func windowDidLoad() {
    super.windowDidLoad()

    if self.contentViewController != nil {
        var vc = self.contentViewController! as NSSplitViewController
        var innerSplitView = vc.splitViewItems[0] as NSSplitViewItem
        var innerSplitViewController = innerSplitView.viewController as NSSplitViewController
        var layerCanvasSplitViewItem = innerSplitViewController.splitViewItems[1] as NSSplitViewItem
        self.layerCanvasViewController = layerCanvasSplitViewItem.viewController as LayerCanvasViewController
    }
}

哪个让我获得了视图控制器(它控制着您看到的下面用红色概述的视图),并在窗口视图控制器中设置了本地属性.

Which gets me the view controller (which controls the view you see outlined in red below) and sets a local property in the window view controller.

因此,现在,我可以直接在响应者链中的文档类中转发工具栏按钮或菜单项事件,因此可以接收我在菜单和工具栏项中设置的操作.像这样:

So now, I can forward the toolbar button or menu item events directly in the document class which is in the responder chain and therefore receives the actions I setup in the menu and toolbar items. Like this:

class LayerDocument: NSDocument {

    @IBAction func addLayer(sender:AnyObject) {
        var windowController = self.windowControllers[0] as MainWindowController
        windowController.layerCanvasViewController.addLayer()
    }

    // ... etc.
}

由于LayerCanvasViewController在加载时被设置为主窗口控制器的属性,因此我可以访问它并调用所需的方法.

Since the LayerCanvasViewController was set as a property of the main window controller when it got loaded, I can just access it and call the methods I need.

这篇关于可可情节提要响应链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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