canPerformAction(_ action: Selector, withSender sender: Any?) 函数无法识别“replace(_:)”操作

问题描述 投票:0回答:1

我需要配置我的 UITextView,使其仅通过“编辑”菜单执行一组选定的操作。我尝试重写 'canPerformAction(_ action: Selector, withSender sender: Any?)' 函数来实现此目的。

class NewTextView: UITextView {

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if action == #selector(paste(_:)) || action == #selector(copy(_:)) || action == #selector(cut(_:)) || action == #selector(select(_:)) || action == #selector(selectAll(_:))
    {
        return super.canPerformAction(action, withSender: sender)
    }
    return false
}

}

但是当我尝试检查“replace(_:)”选择器时,出现以下错误。上面写着“在范围内找不到‘替换’”。 error screenshot.

现在我已经通过检查它的描述来处理它,但我不确定这个方法有多稳健。

if action.description == "replace:"
{
    return true
}
ios swift uitextview selector uiresponder
1个回答
0
投票

标准编辑操作中没有

replace
选择器,因此无论如何它都无法工作。

action.description
检查很好,但如果您想直接在
UITextView
之外检查选择器,则必须指定声明选择器的类型,例如
paste

if action == #selector(UIResponder.paste(_:))
© www.soinside.com 2019 - 2024. All rights reserved.