我需要配置我的 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
}
标准编辑操作中没有
replace
选择器,因此无论如何它都无法工作。
action.description
检查很好,但如果您想直接在UITextView
之外检查选择器,则必须指定声明选择器的类型,例如paste
if action == #selector(UIResponder.paste(_:))