如何禁用 NSTextField 并使其不可编辑? [isEditable 属性 不工作]

How to disable an NSTextField and make it uneditable? [isEditable property not working]

我正在 NSTextField 中输入促销代码,单击“应用”时促销代码已应用。

此时,在按下删除促销代码之前,我的文本字段应该是不可编辑的。如果按下删除促销代码按钮,文本字段将变为可编辑。这就像 Zomato 优惠券代码的应用和删除一样。

我试过 isEditable = false 但这不起作用

@IBOutlet weak var promoCodeValidity: NSTextField!
@IBOutlet weak var promoCode: NSTextField!

func applyCoupon(){
    let couponCode = promoCode.stringValue
    if let offer = bookingView!.applyOffer(offerCode:couponCode){
        promoCodeValidity.stringValue="Offer Applied "+String(offer)+"% Off"
        promoCode.isEditable=false
    }
    else{
        promoCodeValidity.stringValue="Offer Code Invalid"

    }
}

为什么 isEditable 不起作用

add extention:
extension UITextField

{
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.cut(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.select(_:)) || action == #selector(UIResponderStandardEditActions.selectAll(_:))  {
            return false
        }
        return false
}
}

and:
textFiled.isEditable = false 

尝试切换文本字段的 isEnabled 属性,然后更改文本字段的 isEnabled 属性。 即首先禁用文本字段,然后更改文本字段的可编辑属性,然后再次启用文本字段。 希望这能解决您的问题。 我还附上了一个小代码示例:)

class ViewController: NSViewController {
    let textField = NSTextField()
    let button = NSButton()
    override func viewDidLoad() {
        textField.translatesAutoresizingMaskIntoConstraints=false
        textField.placeholderString = "ENTER PROMO CODE"
        button.translatesAutoresizingMaskIntoConstraints=false
        super.viewDidLoad()
        textField.isEditable = true
        button.action = #selector(buttonClicked(_:))
        view.addSubview(textField)
        view.addSubview(button)
        NSLayoutConstraint(item: textField, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: textField, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: textField, attribute: .width, relatedBy: .equal, toItem: nil , attribute: .notAnAttribute, multiplier: 1.0, constant: 100).isActive = true
        NSLayoutConstraint(item: button, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1.0, constant: 0).isActive  = true
        NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: textField, attribute: .trailing, multiplier: 1.0, constant: 20).isActive = true
        NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil , attribute: .notAnAttribute, multiplier: 1.0, constant: 50).isActive = true
        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    @objc func buttonClicked(_ sender: Any) {
        textField.isEnabled = false
        textField.isEditable = false
        textField.isEnabled = true
    }


}