【问题标题】:Cocoa protocol for cut, copy and paste actions用于剪切、复制和粘贴操作的 Cocoa 协议
【发布时间】:2017-02-25 08:26:48
【问题描述】:

Cocoa 中是否有任何协议实现剪切的标准操作:复制粘贴:,就像 UIKit 的 UIResponderStandardEditActions 一样?

我想在这个类中不实现 delete(_:) 的情况下使用新的 Swift3 #selector 来做这样的事情:

override func supplementalTarget(forAction action: Selector, sender: Any?) -> Any? {

    switch action{
        case #selector(delete(_:)):
             return outlineView.delegate

        default:
            return nextResponder
    }
}

谢谢

【问题讨论】:

    标签: swift macos cocoa swift3


    【解决方案1】:

    您可以定义自己的协议:

    @objc protocol MyStandardActionProtocol {
        func cut(_: Any)
        func copy(_: Any)
        func paste(_: Any)
    }
    

    并使用#selector 喜欢:

    override func supplementalTarget(forAction action: Selector, sender: Any?) -> Any? {
        switch action{
        case #selector(MyStandardActionProtocol.cut(_:)):
            return ...
        //...
        default:
            return nextResponder
        }
    }
    

    Selector 实例中不包含类型信息,因此即使没有符合协议的类也可以使用。

    【讨论】:

      猜你喜欢
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      相关资源
      最近更新 更多