Set label title from mouseEntered: on a NSButton

Discussion in 'Mac Programming' started by Val K, Jun 18, 2013.

  1. Val K, Jun 18, 2013
    Last edited: Jun 18, 2013

    macrumors member

    Joined:
    Dec 19, 2012
    Location:
    Italy
    #1
    Hi, i have this code in MyButton.m, subclass of NSButton.

    Code:
    -(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:mad: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.
     
  2. thread starter macrumors member

    Joined:
    Dec 19, 2012
    Location:
    Italy
    #2
    solved

    nevermind, i solved the problem using NSNotificationCenter. Apparently there's no alternative to it.
     

Share This Page