Having a value transform where you bind the view to the entity's property may actually be the most MVC way to separate things. In fact, it's probably the best way to do it because you get two-way binding if you can implement the reverse transform. So, in short, no there's not really a better solution. Just different ones.
But in your situation I might implement another KVC-compliant property on the entity's backing class. Something like displayValue
, and bind to that. I find the code easier to follow if I look up the binding in IB, see that it's to "displayValue" and then just find the appropriate methods.
The issue, though, is that it sounds like faultCorrection
is part of the app or view state, not the instance/entity state, so any displayValue
method would still have to query a singleton somewhere. You said that currently these are static properties. It is likely better to use the bindings-compliant [NSUserDefaultsController sharedUserDefaultsController]
So, all that said, something like this on the entity's class to bind to:
- (NSString*) displayValue
{
NSUserDefaultsController* defaults = NSUserDefaultsController.sharedUserDefaultsController;
NSNumber* faultCorrection = [defaults valueForKeyPath:@"faultCorrection"];
return [NSString stringWithFormat:@"%d",self.errorValue.floatValue - faultCorrection.floatValue];
}
+ (NSSet*) keyPathsForValuesAffectingDisplayValue
{
return [NSSet setWithObject:@"errorValue"];
}
You'd have to figure out how to retrigger the displayValue binding KVO if the user changes the faultCorrection value in the sharedUserDefaults, though. How you do that depends on where the user sets it. And you do have the same issue with any static value that your valueTransformers would currently use-- There's no instance keyPathForValuesAffecting...
method that you can use to register your static values as dependent keypaths. Having each entity instance KVO observe the sharedUserDefaults is probably overkill if you can just blow the ViewController away and reinit it.
Also, as an aside, this is a very similar topic to that covered by this recent question