Copy&Paste with select disabled not showing for UITextField

I have a UITextField outlet declared in my primary view controller that is attached to the "encoder" tab. The "ViewController.swift" is linked to the ViewController. For some reason when a double tap (and or hold) the UITextField, the copy, paste, select/select all options are not showing. They are supposed to show even though the text field module is empty. I tried to implement this:
Code Block
override var canBecomeFirstResponder: Bool {
        return true
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return (action == #selector(UIResponderStandardEditActions.paste(_:)))
}

It gives me the paste option but the thread crashes with this message:
Code Block
Thread 1: "-[String_Conversion_Tool.ViewController paste:]: unrecognized selector sent to instance 0x104e09860"

How can I enable the copy, paste, select/select all options for the UITextField like it was before? or how can I enable them programatically?
Answered by OOPer in 642835022
@S1lent7700

I am not even getting the paste option for an empty UITextField module.

Paste is shown only when the pasteboard is not empty. Are you sure you tested with some text given to the pasteboard?
If the field is empty, you just get paste option.

I implemented and it works in an app (note: I have some issue in simulator, getting
PBItemCollectionServicer connection disconnected
message.

To make it easier, implement a subclass of UILabel:
Code Block
class CopyableLabel: UILabel { }

to wrap all the needed code.
Inside, a showMenu function:
Code Block
@objc func showMenu(sender: AnyObject?) {
self.becomeFirstResponder()
var targetRect = self.bounds // Initialise in case the exact tap position not found
if let gest = sender as? UILongPressGestureRecognizer {
let touchPosition = gest.location(in: self)
targetRect = CGRect(x: touchPosition.x, y: touchPosition.y-20, width: 10, height: 10)
}
let menu = UIMenuController.shared
if !menu.isMenuVisible {
menu.setTargetRect(targetRect, in: self)
menu.setMenuVisible(true, animated: true)
}
}
// This is called in init()
        self.isUserInteractionEnabled = true
        self.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(self.showMenu)))


That's fully described in this thread
https://stackoverflow.com/questions/1246198/
Thank you for the answer. I am working on an iOS 13 project and I am not even getting the paste option for an empty UITextField module.
Could you show the complete code.
Accepted Answer
@S1lent7700

I am not even getting the paste option for an empty UITextField module.

Paste is shown only when the pasteboard is not empty. Are you sure you tested with some text given to the pasteboard?
I did test the paste option when something was copied to the clipboard. It worked naturally...I don't know why I was trying to get it to function when there was nothing on the paste board...
if you setting UITextField textAlignment ? I have some problem when i setting textAlignment = NSTextAlignmentCenter, and i look for some func to reduce this problem Have you solve this bug ?
Copy&Paste with select disabled not showing for UITextField
 
 
Q