Re: Properties, read only and bindings
Re: Properties, read only and bindings
- Subject: Re: Properties, read only and bindings
- From: Steven W Riggins <email@hidden>
- Date: Thu, 7 Aug 2008 11:41:58 -0700
Excellent, even cleaner!
Thanks so much,
Steve
On Aug 6, 2008, at 11:00 PM, Negm-Awad Amin wrote:
Am Do,07.08.2008 um 07:32 schrieb Roland King:
If your isCloning is a property itself, can you use the dependent
key registration logic instead?
[ self setKeys:[ NSArray arrayWithObjects:@"isCloning", nil ]
triggerChangeNotificationsForDependentKey:@"cloningLable" ];
I've never tried this, but it's documented here and I recall
reading about it.
It works and this is what -
setKeys:triggerChangeNotificationForDependentKey: is for - running
under tiger.
The dependent keys mechanism changed in leopard:
+keyPathsForValuesAffectingValueForKey:
+keyPathsForValuesAffecting<Key>
// Simple sample
+ (NSArray*)keyPathForValuesAffectingButtonTitle {
return [NSArray arrayWithObject:@"myState"];
}
Amin
http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/Concepts/DependentKeys.html#/
/apple_ref/doc/uid/20002179
Rols
Steven W Riggins wrote:
Aha!
I was missing the willChangeForKey: call.
- (IBAction) toggleIsCloning: (id) sender
{
[self willChangeValueForKey:@"cloningLabel"];
self.isCloning = !self.isCloning;
[self didChangeValueForKey:@"cloningLabel"];
}
Now it works fine, thanks!
On Aug 6, 2008, at 10:07 PM, Bill Bumgarner wrote:
On Aug 6, 2008, at 9:51 PM, Steven W Riggins wrote:
I'm winding my way through KVO, readonly properties and bindings.
I have an object which has a bool state. I have a method that
toggles that state.
I have a button that calls the method that toggles the state.
The button has a title, which I've bound to a readonly
property, which has a getter that returns a localized label
based on the state.
So when I change the state, obviously the readonly property
doesn't change, and thus the button label does not change.
Changing the readonly property to a normal ivar works fine.
Is there a way to use a synthesized property in this manner, or
is it even proper?
You can solve this in the following two ways (amongst others):
(1) Redeclare the property as readwrite in a class extension in
your .m file and then use dot syntax or the synthesized setter/
getter to set the value.
I.e.
.h:
@interface Foo : NSObject
{
BOOL f;
}
@property(readonly, nonatomic) BOOL f;
@end
.m
@interface Foo()
@property(readwrite, nonatomic) BOOL f;
@end
@implementation Foo
@synthesize f;
// ... use the setter and getter as needed
@end
(2) Call willChange/didChange before/after you change the value:
- (void) toggle: sender
{
.... logic ...
[self willChangeValueForKey: @"f"];
f = ... new state ...;
[self didChangeValueForKey: @"f"];
}
b.bum
_______________________________________________
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
_______________________________________________
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
Amin Negm-Awad
email@hidden
_______________________________________________
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