View Full Version : How to bind a Label's Value in IB to a class dynamically created by my AppController

Jul 26, 2009, 08:18 AM
Hi guys,

I'm pretty new to Objective-C, XCode and Interface Builder and seem to be running into a little problem. I hope someone here on the forum will be able to help me out with this.

I have the following class :

@interface LoginClass : NSObject
NSString *userName;
NSString *password;
NSString *secret;
BOOL connected;

- (void)logIn;
- (void)logOut;

- (id)initWithLogin:(NSString *)aLogin password:(NSString *)aPassword;

@property (readwrite, copy) NSString *userName;
@property (readwrite, copy) NSString *password;
@property (readonly) NSString *secret;
@property (readonly) BOOL connected;

I'm creating an instance of this class in my app controller and seem to be able to use it, since I can dump it to the NSLog and see the properties get updated, ...

My AppController looks like this :

@interface AppController : NSObject
IBOutlet NSTextField *userName;
IBOutlet NSTextField *password;
// IBOutlet NSTextField *secret;
IBOutlet NSWindow *window;

LoginClass * LoginClass;

- (IBAction)connect:(id)sender;

And the Connect implementaion looks like this :

- (IBAction)connect:(id)sender
// Create an instance of our LoginClass
LoginClass = [[LoginClass alloc] initWithLogin:[userName stringValue] password:[password stringValue]];

// Now that we have our LoginClass we can start to work with it.
[LoginClass logIn];


In the logIn implementation I'm setting the Secret string to something, which seems to be working since everything gets logged perfectly using NSLog. I connected the connect method to a button in Interface Builder, and that is working perfectly.

Now I'm wondering if there is some way I can bind the value of a Label to that secret property of my LoginClass ? I've been trying almost everything but can't seem to get it working.

I added an object in IB and set it to my LoginClass and tried to bind the value of the label to the secret key of that object, but that didn't seem to work (no errors, value simply doesn't get displayed).

Tried binding it to the AppController using LoginClass.secret as the key, but that doesn't seem to be working neither (no errors, value simply doesn't get displayed).

I'm probably doing something wrong, or am missing something, so I hope someone here will be able to help me out.

Thanks a lot in advance,


Jul 26, 2009, 12:27 PM
Well you made the property read only... so not the way you're doing it.

Maybe make it part of the init method, so that it is something like...

[LoginClass initWithLogin:[userName stringValue] password:[password stringValue] secret: [secret stringValue]];

Then obviously have your init method set the value to the ivar NSString.

If your reasoning for not doing this is because you are worried that someone can see or change the value, wouldn't they be able to see or change it in the serialized xib file?

EDIT: Oh it seems like you want to set the AppControllers secret to the value of secret in your LoginClass.
I don't know how to "bind" it to it, but right after you init or send it the logIn message you could put in
[secret setStringValue: LoginClass.secret ];

Jul 26, 2009, 05:51 PM

Well, actually I had it almost right, I did have it setup correctly only thing I was missing was :

// Now that we have our LoginClass we can start to work with it.
[self willChangeValueForKey:@"LoginClass"];
[LoginClass logIn];
[self didChangeValueForKey:@"LoginClass"];

Each time I change something in the LoginClass I need to embed it in the willChangeValueForKey and didChangeValueForKey calls. A bit cumbersome but once I added that everything was working perfectly.