Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

donaghy

macrumors member
Original poster
Aug 6, 2009
49
0
Hi all, Im following through the book Cocoa Programming for Mac OSX and working on challenge Chapter 7. Key-Value Coding; Key-Value Observing. Im trying to bind the key value to the slider but i cant see the key value in the list. Here's my code:

@interface AppController : NSObject
{
int fido;
}
- (int)fido;
- (void)setFido:(int)x;
@end

#import "AppController.h"

@implementation AppController

- (id)init
{
[super init];
[self setValue:[NSNumber numberWithInt:5]
forKey:mad:"fido"];
NSNumber *n = [self valueForKey:mad:"fido"];
NSLog(@"fido = %@", n);
return self;
}

- (int)fido
{
NSLog(@"-fido is returning %d", fido);
return fido;
}

- (void)setFido:(int)x
{
NSLog(@"-setFido: is called with %d", x);
fido = x;
}

@end


So when i select the slider control, go to binding inspector and bind to my AppController i should see fido in the Model Key Path combo list but i can not. Can someone point out what the issue is here?

Cheers.
 
Hi Donaghy

I am not at home so I can not verify exact this challange, but here are some comments:

I have noticed myself some times that when binding a progress bar to a value I could not pick up the variable key name directly form a list. I just written it down and it worked OK.


A good practice is to use properery...

@property (readwrite,assign) int fido;

This will ansure that you will not need to write manual declaration of "setFido" and "fido"


In order that your code should be compatible with Key Value Observing
you should write your setter in the following way:


Code:
- (void)setFido:(int)x
{
  [self willChangeValueForKey:@"fido"];
  fido = x;
  [self didChangeValueForKey:@"fido"];
}

It will make it the notification possible to objects that bind into your value.
The notification will be generated even if you change the value in the way you did in the init method.

If you still have problems I will try to look at it tonight
GL
/petron
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.