PDA

View Full Version : Change Button's Title problem :(((




Sergio10
Apr 25, 2009, 04:41 PM
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:

@interface MyController : NSObject
{
IBOutlet NSString *myValue;
}
- (void)awakeFromNib;
@end


@implementation MyController
- (void)awakeFromNib
{
myValue = @"Hello World";
}
@end

And screenshot:
http://pic.ipicture.ru/uploads/090426/thumbs/Lz2eg22T6y.png (http://ipicture.ru/Gallery/Viewfull/17965094.html)
Please help me.
Thanks



Catfish_Man
Apr 25, 2009, 09:48 PM
Your IBOutlet needs to be the button, not a string. You need to call methods on the button to set its title.

kpua
Apr 25, 2009, 10:54 PM
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.

eddietr
Apr 25, 2009, 11:02 PM
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:


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


@property (nonatomic, copy) NSString* myValue;


Then add to the implementation:


@synthesize myValue;


then in your awakeFromNib:


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.

kainjow
Apr 25, 2009, 11:47 PM
Or if you were lazy you could just do:
[self setValue:@"Hello World" forKey:@"myValue"];
But I'm not recommending that ;)

Sergio10
Apr 26, 2009, 08:52 AM
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)?