使用IBInspectable禁用UITextField上的剪切,复制和粘贴

时间:2016-01-11 12:46:39

标签: ios swift swift2 ibdesignable

请找到以下不适合我的代码。

 @IBInspectable var pasteOption: Bool = true {
        didSet {
            func canPerformAction(action: Selector, withSender sender: AnyObject) -> Bool {
                if action == "selectAll:" {
                    return pasteOption
                }
                if action == "select:" {
                    return pasteOption
                }
                if action == "cut:" {
                    return pasteOption
                }
                if action == "copy:" {
                    return pasteOption
                }
                if action == "paste:" {
                    return pasteOption
                }
                return super.canPerformAction(action, withSender: sender)
            }
        }
    }

我想使用UITextfield禁用剪切,复制和粘贴IBInspectable

1 个答案:

答案 0 :(得分:1)

您需要像这样定义您的var:

@IBInspectable var pasteOption: Bool = true

然后覆盖UITextField的{​​{1}}函数,如下所示:

canPerformAction

通过这样做,您可以为函数中定义的特定操作返回override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool { if action == "selectAll:" { return pasteOption } if action == "select:" { return pasteOption } if action == "cut:" { return pasteOption } if action == "copy:" { return pasteOption } if action == "paste:" { return pasteOption } return super.canPerformAction(action, withSender: sender) } 的值(pasteOptionselectAllselectcut并且在这种情况下为copy)任何时候您的文本字段都会打开编辑菜单。