1

I am having an issue with refreshing a text edit field in an AppKit (NS) window. I'm coming from long experience with UIKit to AppKit, so I'm still banging into some guardrails.

I have a base settings class, like so (all this is pcode. I can point to the actual code, but it's a bit wordy for an SO question):

class A: NSObject {
    var liveDataObject: Int = 0
    func reset() { liveDataObject = 0 }
}

class B: A {
    @objc dynamic var liveDataObjectAccessor: Int {
        get { return liveDataObject }
        set { liveDataObject = newValue }
    }
}

Now, if I use the IB Bindings to associate an instance of B, and its liveDataObjectAccessor calculated property with an NSTextField, it works great. Changes to the text field are reflected in the data.

If I then have a "RESET" button that calls the A.reset() method, I would expect the text field to reflect 0 (if it had been changed by the user, say they entered "1").

However, what I am experiencing, is that the A.liveDataObject is set to 0, but the text field continues to display "1".

How do I make sure the text field gets updated when the underlying value is changed?

Chris Marshall
  • 4,910
  • 8
  • 47
  • 72
  • I am thinking that I may need to override the reset() method in B, and do something like liveDataObjectAccessor = liveDataObject for each of my observable values. Klunky, but I'll bet it works. I'd much rather have something that just forced the UI elements to refresh their bindings. Be aware that the UI objects have no linkage to the code. It's all done in IB. – Chris Marshall Aug 21 '19 at 16:14
  • Nah...that doesn't work. "Welp, back to the old drawing board." -Wile E. Coyote – Chris Marshall Aug 21 '19 at 16:20
  • Ugh. I just saw [this question](https://stackoverflow.com/questions/15480668/how-to-force-update-cocoa-bindings). Looks like I will have to basically obviate the bindings, and actually force-update the text. – Chris Marshall Aug 21 '19 at 16:26

0 Answers0