本文介绍了可可:将动作(复制:,粘贴:等)转发到响应者链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSOutlineView 的子类,该子类实现了 copy: paste: cut:等。
另外, NSDocument 子类实现了相同的方法。

I have a subclass of NSOutlineView that implements copy:, paste:, cut: etc.Also, the NSDocument subclass implements these same methods.

当大纲视图位于响应者链中(是第一响应者还是其父视图)时,所有复制/粘贴事件都会被 NSOutlineView 子类。我想要的是根据上下文捕获某些消息,或者让它们传播并由 NSDocument 子类捕获。

When the outline view is in the responder chain (is first responder or a parent view of it), all copy/paste events are intercepted by the NSOutlineView subclass. What I want, is depending on the context catch some of these messages, or let them propagate and be caught by the NSDocument subclass.

我想要的基本上是:

- (void)copy:(id)sender
{
    // If copy paste is enabled
    if ([self isCopyPasteEnabled]) {
        [[NSPasteboard generalPasteboard] clearContents];
        [[NSPasteboard generalPasteboard] writeObjects:self.selectedItems];
        return;
    }

    // If copy paste is disabled
    // ... forward copy: message to the next responder,
    // up to the NSDocument or whatever
}

我已经尝试了很多技巧,但都没有成功:

I've already tried many tricks, none was successful:


  • [[self nextResponder] copy:sender] 无效,因为下一个响应者可能没有实现 copy:

  • [super copy:sender] 相同, super不会实现 copy:

  • [NSApp sendAction:anAction to:nil from:sender] 可以将操作发送给第一响应者。如果在动作中使用

  • [[self nextResponder] copy:sender] that doesn't work because the next responder may not implement copy:
  • [super copy:sender] same here, super doesn't implement copy:
  • [NSApp sendAction:anAction to:nil from:sender] this is nice to send an action to the first responder. If used inside an action

我当然可以手动在响应者链上循环,直到找到响应的东西复制:甚至直接在当前文档上调用复制:,但我正在寻找正确的方法。

Sure I could manually loop on the responder chain until I find something that responds to copy: or even directly call copy: on the current document, but I'm looking for the right way of doing it.

非常感谢!

推荐答案

这应该有效:

[[self nextResponder] tryToPerform:_cmd with:sender];

尽管有一个问题:响应者链中存在实现<$ c $的响应者c> -copy:本身将导致复制菜单项被启用,即使您的对象不在链中或未实现复制:。您的对象可以使用 -validateMenuItem: -validateUserInterfaceItem:禁用该项目,但是如果和只有在链中还有另一个潜在目标并且该目标可以启用该项目的情况下。

There's a problem, though: the presence of a responder in the responder chain which implements -copy: will, in and of itself, cause the Copy menu item to be enabled even if it wouldn't otherwise be if your object weren't in the chain or didn't implement -copy:. Your object could disable that item using -validateMenuItem: or -validateUserInterfaceItem:, but it will be nontrivial to enable if and only if there's another potential target up the chain and that target would enable the item.

另一种方法是只搜索实现了action方法的响应者您的大纲视图(如果禁用剪贴板支持)。覆盖 -respondsToSelector:。如果选择器是粘贴板操作之一,并且您的粘贴板支持被禁用,则即使您的类确实实现了它,也返回false。也就是说,撒谎并声称您的对象只是不响应那些选择器。对于任何其他选择器,或者如果您的粘贴板支持处于启用状态,请通过调用super并返回其返回值。

A different approach is to just make the search for a responder that implements the action method skip your outline view if you disable pasteboard support. Override -respondsToSelector:. If the selector is one of the pasteboard operations and your pasteboard support is disabled, return false even though your class really does implement it. That is, lie and claim your object just doesn't respond to those selectors. For any other selector, or if your pasteboard support is on, call through to super and return what it returns.

这篇关于可可:将动作(复制:,粘贴:等)转发到响应者链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:43