I'm getting the promo code input in NSTextField and on clicking apply the promo code is applied.
At this point, my text field should be uneditable until the remove promo code is pressed. if remove promo code button is pressed the text field becomes editable. This works just like Zomato coupon codes apply and remove.
I tried isEditable = false but that doesn't work
@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"
}
}
why isEditable doesn't work
Try toggling the isEnabled property of the text field and then change the isEnabled property of the text field. ie First disable the text field, then change the editable property of the text field and then again enable the text field. Hope this solves your problem. I have attached a small code sample also :)
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.