繁体   English   中英

如何禁用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不起作用

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

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
    }


}
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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM