Skip navigation.
 
mlRe: basic bindings question
FROM : Ken Thomases
DATE : Thu May 15 06:09:49 2008

On May 14, 2008, at 10:54 PM, Daniel Child wrote:
> If I want to observe the property of an object, and in my case, it 
> turns out that the M and C are collapsed into a model-controller, 
> then it's basically a case of asking something to observe itself.


Well, it is sometimes desirable for an object to observe itself for 
changes to its properties.  However, it is often not necessary.  In 
particular, an object's properties should only be modified by 
messaging the object, which gives you a perfect way to "notice" that 
the change is being made -- right in the method which is invoked by 
the message.

For example, if your controller's number property is being changed, 
then something should be invoking its -setNumber: method.  In that 
case, you can just add your code for coping with a change to that 
method:

- (void) setNumber:(NSNumber*)newNumber
{
   if (newNumber != number && ![number isEqualToNumber:newNumber])
   {
       [number release];
       number = [newNumber retain];
       // Add code to cope with a change in the number property here
   }
}


Can you be more specific about why you want your controller to 
observe its own number property?  What are you trying to accomplish? 
I suspect there's another way to accomplish what you're interested in.


> In other words, your "myFoo addObserver: self <THE CONTROLLER> 
> forKeyPath: @"number" becomes....
>
> - (void) awakeFromNib {
>     [self addOberver: self forKeyPath: @"number" options: 0 context: 
> NULL];


There's a typo there.  You've missed the "s" in "addObserver".

> }
>
> I tried that and got the same message as before (<receiver> may not 
> respond to addObserver), only this time the receiver is the 
> controller instead of number.


Can you report the exact message you're getting?  Is it a compile-
time warning or a run-time exception?


> Is it not possible to collapse M and C for a case where you want to 
> track a simple text field. It seems it must be.


It is possible.  We'll have to see what's going wrong in your 
particular case.

Cheers,
Ken

Related mailsAuthorDate
mlbasic bindings question Daniel Child May 14, 21:08
mlRe: basic bindings question Ken Thomases May 14, 23:11
mlRe: basic bindings question Daniel Child May 15, 05:54
mlRe: basic bindings question Ken Thomases May 15, 06:09
mlRe: basic bindings question Daniel Child May 15, 06:43
mlRe: basic bindings question Ken Thomases May 15, 06:54
mlRe: basic bindings question Daniel Child May 16, 04:47
mlRe: basic bindings question Ken Thomases May 16, 05:33