簡體   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