Bindings & Reverting Properties
Bindings & Reverting Properties
- Subject: Bindings & Reverting Properties
- From: Dave Keck <email@hidden>
- Date: Fri, 21 Aug 2009 03:27:12 -1000
Hey list,
I've implemented a custom bindings-compatible preferences controller object
- let's call it MyPrefsController. It's similar to NSUserDefaultsController,
but has lots of different functionality, and inherits directly from
NSObject.
As the user is changing the preferences in the UI, the preferences file
on-disk is kept in sync. But due to the nature of the app, sometimes saving
the preferences will fail. If this is the case, the UI control should simply
revert back to its previous value before the attempted save. It's this
revert that's giving me trouble.
Here's a vastly over-simplified setter method that displays my problem.
Suppose a checkbox in the UI is bound to the 'happy' property, which has the
following setter:
- (void)setHappy: (BOOL)newHappy
{
happy = newHappy;
if (![self save])
self.happy = !happy; // Revert to the old value. Pretend the
recursive call wouldn't attempt //
another save and cause a stack overflow and crash. It just needs
// to revert the value while sending
the appropriate KVO notifications.
}
If saving fails, the 'happy' property gets reverted. But this doesn't get
reflected for the checkbox in the UI - it still shows the new value as if no
error occurred. One way around this is to schedule 'self.happy = !happy' to
be executed in the next iteration of the run loop. But that's kludgy.
The KVC validation methods seem like a possibility, but they're for
validating the value before it's applied to the model, rather than reverting
the value of the property after-the-fact.
Is there a right way to do this?
Thanks!
David
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden