UITextView 中的 canPerformAction(_ action: Selector, withSender sender: Any?) 函数无法识别 'replace(_:)' 操作

canPerformAction(_ action: Selector, withSender sender: Any?) function in UITextView does not recognize 'replace(_:)' action

提问人:Sundara Aadithyan 提问时间:9/20/2023 最后编辑:Sundara Aadithyan 更新时间:9/28/2023 访问量:70

问:

我需要配置我的 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(_:)”选择器时,出现以下错误。上面写着“在范围内找不到'替换'”。错误屏幕截图。

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

if action.description == "replace:"
{
    return true
}
iOS Swift UITtextView 选择器 UIStesterer

评论


答:

0赞 vadian 9/28/2023 #1

标准编辑操作中没有选择器,因此它无论如何都无法工作。replace

检查是可以的,但是如果你想直接在符合的类之外检查选择器,你必须指定声明选择器的类型,例如action.descriptionUIViewpaste

if action == #selector(UIResponder.paste(_:))

评论

0赞 Sundara Aadithyan 10/3/2023
感谢您的回复。replace 函数在 UITextInput 协议中声明。由于它未声明为可选函数,因此我无法检查其选择器。
0赞 vadian 10/3/2023
明白了。但正确的签名是#selector(UITextInput.replace(_:withText:))