In Interface Builder, how do I bind to a property of a class via another class?

815 views Asked by At

OSX desktop ap in Objective-C...

I think this is a basic question, but even after spending a good deal of time with the KVO guide and various related questions I can't quite wrap my brain around what's going on in this particular case (alternatively, this is just bad form and I should stop)

  1. Create a custom class, let's call it "OtherClass" and give it a property:

    @interface OtherClass : NSObject{}
    @property NSString* aString;
    
  2. Inside a ViewController, create an instance of this custom class

    oc = [[OtherClass alloc] init];
    
  3. In the interface builder associated with the ViewController class, bind a field like so:enter image description here

What I'd like to happen is that whenever something like [oc setAString:@"SomeValue"] is called, the UI is updated, but the text field does not update when the value changes.

For what it's worth, the actual use case is a singleton class which is a manager for an external piece of hardware. I would like to be able to have other parts of my program (inside the code for the hardware, for instance) update a status string when the hardware status changes. It would be super nice if I could just bind a UI field to "viewController.hardwareMgr.status" and then call "[hardwareMgr setStatus:@"allGood"]" wherever I like, and be done with it.

I do understand conceptually that the problem is the nib isn't receiving the update notifications but I'm not sure the best way to fix this. I can of course manually register a listener on the viewController, for instance, and could use this to "proxy" updates to a local property on the viewController, but this seems awkward at best. As my manager class is a singleton, I could also create another "instance" inside IB and bind to this, but this seems awkward for different reason and not applicable to non-singleton cases.

How to accomplish this, or is this pattern simply a bad idea?

0

There are 0 answers