Change Button's Title problem :(((

Discussion in 'Mac Programming' started by Sergio10, Apr 25, 2009.

  1. Sergio10 macrumors regular

    Joined:
    Oct 3, 2007
    #1
    Hi!

    I can't update button's title using outlet. But it don't works((( Does it XCode 3.1 bug?

    Here is a code:
    PHP:
    @interface MyController NSObject
    {
        
    IBOutlet NSString *myValue;
    }
    - (
    void)awakeFromNib;
    @
    end
    PHP:
    @implementation MyController
    - (void)awakeFromNib
    {
        
    myValue = @"Hello World";
    }
    @
    end
    And screenshot:
    [​IMG]
    Please help me.
    Thanks
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    Your IBOutlet needs to be the button, not a string. You need to call methods on the button to set its title.
     
  3. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #3
    He's trying to bind to the button title to the controller's string.

    You need to either make it an @property, or manually define KVC-compliant "myValue"/"setMyValue:" (or whatever) methods and within -awakeFromNib, change the value through the dot syntax or by calling setMyValue: directly. That will trigger the binding and change the button's title.
     
  4. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #4
    So I see you are using bindings.

    So the reason this isn't working for you is your controller object is not KVO compliant for that property.

    One way to make this work is to define:

    Code:
    - (void) setMyValue: (NSString*) aString {...}
    
    and then use that method to set your value.

    Even easier is to define myValue as a property and let the compiler synthesize this setter method for you.

    So in the interface you add this:

    Code:
    @property (nonatomic, copy) NSString* myValue;
    
    Then add to the implementation:

    Code:
    @synthesize myValue;
    
    then in your awakeFromNib:

    Code:
    self.myValue = @"This Works!";
    
    // or [self setMyValue:@"This Works, too"];
    
    And you should see the button text change.

    Hope that helps. The lesson here is that bindings require objects that are Key/Value Observing (KVO) compliant. And KVO compliance needs Key/Value Coding (KVC) compliance to work automatically.

    EDIT: Never mind, I see kpua already answered this for you.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Or if you were lazy you could just do:
    Code:
    [self setValue:@"Hello World" forKey:@"myValue"];
    But I'm not recommending that ;)
     
  6. Sergio10 thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #6
    Thanks, for replying!!!

    One more think: How to set title(e.g. for a button control) if I know only object id (e.g. 26)?
     

Share This Page