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

Discussion in 'Mac Programming' started by Annunaki, Jul 26, 2009.

  1. macrumors newbie

    Joined:
    Aug 9, 2006
    Location:
    Belgium
    #1
    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 :

    Code:
    @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 :

    Code:
    @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 :

    Code:
    - (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,



    Stefaan
     
  2. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #2
    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...
    Code:
    [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 ];
     
  3. thread starter macrumors newbie

    Joined:
    Aug 9, 2006
    Location:
    Belgium
    #3
    Hi,

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

    Code:
    	// 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.


    Regards,



    Stefaan
     

Share This Page