A label in a XIB bound to an NSMutableString property does not seem to update when I change the string, but the label does update if the property is NSString.
In a test app, I have the default AppDelegate class and MainMenu.xib. I create two properties in AppDelegate, one NSString and one NSMutableString, and bind them to two labels in the XIB. I have two buttons to change the values of these strings from one set to another and back. The code is given below. The output of NSLog shows that the value of NSMutableString is changing, but is not being reflected in the GUI.
Not sure what I am missing.. any help will be appreciated!
PS: EDIT: I want to achieve this without creating a new mutable string
CODE:
@synthesize mutLabel, unmutLabel;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self willChangeValueForKey:@"mutLabel"];
mutLabel = [NSMutableString stringWithCapacity:10];
[mutLabel setString:@"MutLabel 1"];
[self didChangeValueForKey:@"mutLabel"];
[self willChangeValueForKey:@"unmutLabel"];
unmutLabel = @"UnMutLabel 1";
[self didChangeValueForKey:@"unmutLabel"];
[self addObserver:self forKeyPath:@"mutLabel" options:0 context:nil];
[self addObserver:self forKeyPath:@"unmutLabel" options:0 context:nil];
}
- (IBAction)clkBtn1:(id)sender {
[self willChangeValueForKey:@"mutLabel"];
[mutLabel setString:@"MutLabel 1"];
[self didChangeValueForKey:@"mutLabel"];
[self willChangeValueForKey:@"unmutLabel"];
unmutLabel = @"UnMutLabel 1";
[self didChangeValueForKey:@"unmutLabel"];
}
- (IBAction)clkBtn2:(id)sender {
[self willChangeValueForKey:@"mutLabel"];
[mutLabel setString:@"MutLabel 2"];
[self didChangeValueForKey:@"mutLabel"];
[self willChangeValueForKey:@"unmutLabel"];
unmutLabel = @"UnMutLabel 2";
[self didChangeValueForKey:@"unmutLabel"];
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"Key change: Key: %@ Value: %@\n",keyPath, [self performSelector:NSSelectorFromString(keyPath)] );
}