【问题标题】:Disable cut, copy, paste on UITextField using IBInspectable使用 IBInspectable 在 UITextField 上禁用剪切、复制、粘贴
【发布时间】:2016-01-11 12:46:39
【问题描述】:

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

 @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 禁用剪切、复制、粘贴功能。

【问题讨论】:

标签: ios swift swift2 ibdesignable


【解决方案1】:

你需要像这样定义你的变量:

@IBInspectable var pasteOption: Bool = true

然后像这样覆盖您的UITextFieldcanPerformAction 函数:

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)
}

通过这样做,您可以随时为函数中定义的特定操作(在这种情况下为 selectAllselectcutcopypaste)返回 pasteOption 的值您的文本字段会打开一个编辑菜单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多