【问题标题】:How to disable an NSTextField and make it uneditable? [isEditable property not working]如何禁用 NSTextField 并使其不可编辑? [isEditable 属性不起作用]
【发布时间】:2019-06-15 21:48:14
【问题描述】:

我在 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 不起作用

【问题讨论】:

    标签: swift macos cocoa nstextfield


    【解决方案1】:

    尝试切换文本字段的 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
        }
    
    
    }
    

    【讨论】:

    • 很好的答案谢谢
    • 很高兴它有帮助:)
    【解决方案2】:
    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 
    

    【讨论】:

    • 你能给MACOS这个我要求NSTextField吗
    猜你喜欢
    • 2014-12-21
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2018-12-24
    相关资源
    最近更新 更多