Re: Stuck in basic bindings
Re: Stuck in basic bindings
- Subject: Re: Stuck in basic bindings
- From: Francis Derive <email@hidden>
- Date: Tue, 17 Jan 2006 22:09:05 +0100
On Jan 17, 2006, at 7:56 PM, Miguel Sanchez wrote:
Francis,
Note that setKeys: triggerChangeNotificationsForDependentKey: does
NOT automatically update the state of the object for you, it just
says that a change in one key should also trigger a notification
that another key changed, so that if a view is bound to your object
it is told to redisplay the value. But you have to change the value
within the code of your object.
Miguel,
From what I read from Apple's documentation :
There are many situations in which the value of one property depends
on that of one or more other
properties. If the value of one attribute changes, then the value of
the derived property should also
be flagged for change. To ensure that key-value observing
notifications are posted for these dependent
properties, you should use
setKeys:triggerChangeNotificationsForDependentKey: to trigger
notifications automatically.
(Model Object Implementation Guide).
I understand that with my implementation of the derived property:
- (float) mouseXlocation {
return [self mouseLocation].x;
}
mouseXlocation should be executed upon - *automatic* - notification
of a change in the value of the property mouseLocation.
It does seem to me that I follow with mouseXlocation the same
pattern as fullName in the example provided in Apple's Key-Value
Observing programming Guide :
Listing 1 Registering a dependent key
+ (void)initialize {
[self setKeys:[NSArray arrayWithObjects:@"firstName",@"lastName",nil]
triggerChangeNotificationsForDependentKey:@"fullName"];
}
- (NSString *)fullName {
return [NSString stringWithFormat:@"%@ %@",
[self firstName],[self lastName]];
}
[...]
In your mouseDown method, the second NSLog call references
mouseXlocation and mouseYlocation which are never set, so their
values will still be 0. Do you even need to declare mouseXlocation
and mouseYlocation? You can just remove those and change your NSLog
call to
NSLog(@"fdeGradientView -- mouseDown - mouseXlocation:%f
mouseYlocation:%f", [self mouseXlocation], [self mouseYlocation]);
Perfectly, you are right, this is the point : I must not - if I
follow all the examples I see - declare a mouseXlocation instance
variable.
And having the NSLog as you mention is the right way to do :
[Session started at 2006-01-17 21:51:25 +0100.]
2006-01-17 21:51:31.566 fdeGradientViewUser[3316] FDGradientViewUser
-- testerXlocation:0.000000
2006-01-17 21:51:31.623 fdeGradientViewUser[3316] FDGradientViewUser
-- testerXlocation:0.000000
2006-01-17 21:51:31.625 fdeGradientViewUser[3316] FDGradientViewUser
-- testerXlocation:0.000000
2006-01-17 21:51:31.627 fdeGradientViewUser[3316] FDGradientViewUser
-- testerXlocation:0.000000
2006-01-17 21:51:33.809 fdeGradientViewUser[3316] fdeGradientView --
mouseDown - mouseLocation.x:253.000000 mouseLocation.y:83.000000
2006-01-17 21:51:33.809 fdeGradientViewUser[3316] fdeGradientView --
mouseDown - mouseXlocation:253.000000 mouseYlocation:83.000000
2006-01-17 21:51:33.911 fdeGradientViewUser[3316] fdeGradientView --
mouseUp - mouseLocation.x:253.000000 mouseLocation.y:83.000000
2006-01-17 21:51:33.911 fdeGradientViewUser[3316] fdeGradientView --
mouseUp - mouseXlocation:253.000000 mouseXlocation:83.000000
The last problem is that the last step is not reached. In the IB
Bindings Inspector :
the mouseXlocation property is bound to a testerXlocation
property : mouseXlocation = selection.testerXlocation [UserCtrl
(NSObjectController)].
a NSTextField is bound to the same testerXlocation property : value
= selection.testerXlocation [UserCtrl (NSObjectController)]
but the NSTextField remains at "0" in the interface.
Having :
@interface FDGradientViewUser : NSObject {
float testerXlocation;
float testerYlocation;
}
with :
- (float) testerXlocation {
NSLog(@"FDGradientViewUser -- testerXlocation:%f", testerXlocation);
return testerXlocation;
}
- (void) setTesterXlocation:(float) theX {
NSLog(@"FDGradientViewUser -- setTesterXlocation -- theX:%f", theX);
testerXlocation = theX;
NSLog(@"FDGradientViewUser -- testerXlocation:%f", testerXlocation);
}
In the console window, I don't see any trace of these NSLog ...
Thanks for the help you have already given to me.
Cheers,
Francis.
- Miguel
On Jan 17, 2006, at 8:26 AM, Francis Derive wrote:
Bonjour la liste,
I have been working a big while ( subject = "Binding with value
transformer enigma" ) and I would appreciate some help from the list.
I have changed the architecture to make it simpler and more sound
to me, but still a problem.
From a custom palette, I get a view and I want to display the x
and y coordinates of the mouse downs, drags, and ups.
@interface fdeGradientView : NSView {
[...]
NSPoint mouseLocation;
float mouseXlocation;
float mouseYlocation;
}
It is not Core Data.
I use "Basic Accessor Methods" for mouseLocation ivar ( because
simple NSPoint type).
I no more bind from the NSPoint to a corresponding instance
variable of a tester model object.
Rather, I make immediately mouseXlocation and mouseYlocation
depend on mouseLocation :
- I declare the dependency in the view +initialize
- in the same +initialize, I expose the mouseXlocation and
mouseYlocation for binding
+ (void)initialize {
NSArray *keys = [NSArray arrayWithObjects:@"mouseLocation", nil];
[self setKeys:keys
triggerChangeNotificationsForDependentKey:@"mouseXlocation"];
[self setKeys:keys
triggerChangeNotificationsForDependentKey:@"mouseYlocation"];
[...]
[self exposeBinding:@"mouseXlocation"];
[self exposeBinding:@"mouseYlocation"];
}
In fact, I have not to go far away from the view, because it
appears that the dependent properties mouseXlocation and
mouseYlocation are not set in the view:
[Session started at 2006-01-17 16:44:58 +0100.]
2006-01-17 16:45:01.758 fdeGradientViewUser[2564]
FDGradientViewUser -- testerXlocation:0.000000
2006-01-17 16:45:01.784 fdeGradientViewUser[2564]
FDGradientViewUser -- testerXlocation:0.000000
2006-01-17 16:45:01.788 fdeGradientViewUser[2564]
FDGradientViewUser -- testerXlocation:0.000000
2006-01-17 16:45:01.808 fdeGradientViewUser[2564]
FDGradientViewUser -- testerXlocation:0.000000
2006-01-17 16:45:03.906 fdeGradientViewUser[2564] fdeGradientView
-- mouseDown - mouseLocation.x:231.000000 mouseLocation.y:74.000000
2006-01-17 16:45:03.907 fdeGradientViewUser[2564] fdeGradientView
-- mouseDown - mouseXlocation:0.000000 mouseYlocation:0.000000
<<<<<<<<<<<<<<
2006-01-17 16:45:03.974 fdeGradientViewUser[2564] fdeGradientView
-- mouseUp - mouseLocation.x:231.000000 mouseLocation.y:74.000000
2006-01-17 16:45:03.976 fdeGradientViewUser[2564] fdeGradientView
-- mouseUp - mouseXlocation:0.000000 mouseXlocation:0.000000
<<<<<<<<<<<<<<<<
with
- (void) mouseDown:(NSEvent *)event {
mouseLocation = [self convertPoint:[event locationInWindow]
fromView:nil];
NSLog(@"fdeGradientView -- mouseDown - mouseLocation.x:%f
mouseLocation.y:%f", mouseLocation.x, mouseLocation.y);
NSLog(@"fdeGradientView -- mouseDown - mouseXlocation:%f
mouseYlocation:%f", mouseXlocation, mouseYlocation);
[self setNeedsDisplay:YES];
}
and
- (float) mouseXlocation {
return [self mouseLocation].x;
}
I thank you enormously for any progress I could make on this
damned promising binding subject.
Cheers,
Francis.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden