PDA

View Full Version : Set label title from mouseEntered: on a NSButton




Val K
Jun 18, 2013, 06:23 AM
Hi, i have this code in MyButton.m, subclass of NSButton.

-(void)updateTrackingAreas{
if (trackingArea){
[self removeTrackingArea:trackingArea];
}
NSTrackingAreaOptions options = NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow;
trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
}
-(void)mouseEntered:(NSEvent *)theEvent{
NSLog(@"ENTER");
[myTextField setStringValue:@"Foo"];
NSLog(@"label: %@", myTextField.stringValue);
}
-(IBAction)changeStringValue:(id)sender{
[myTextField setStringValue:@"Foo"];
}

If i call the changeStringValue IBAction everything goes fine, but nothing happens hovering the NSButton associated with the MyButton class, it only shows the NSLog "ENTER" message but myTextField stringvalue isn't updated and the "label:" nslog says NULL.
Does anyone have a clue?

Edit:
I've inserted [self performSelectorOnMainThread:@selector(changeStringValue: ) withObject:myTextField waitUntilDone:YES]; inside the mouseEnter: event and NSLog(@"Running"); inside the changeStringValue function: now hovering the button i call the function (i see the "running" log) but nothing happens. The function, as a Sent Action assigned to a button, is still working. I've also tried with waitUntilDone:YES/NO and withObject:myTextField/self but it's the same.



Val K
Jun 19, 2013, 03:39 AM
nevermind, i solved the problem using NSNotificationCenter. Apparently there's no alternative to it.