3

Please find the following code which did not work for me.

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

I want to disable cut, copy, paste on my UITextfield using IBInspectable.

Janak Thakkar
  • 414
  • 5
  • 20

1 Answers1

1

You need to define your var like this:

@IBInspectable var pasteOption: Bool = true

and then override your UITextField's canPerformAction function like this:

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

By doing this you return the value of pasteOption for the specific actions defined in the function (which are selectAll, select, cut, copy and paste in this case) any time your text field opens up an edit menu.

bcamur
  • 894
  • 5
  • 10