Question

So I am trying to make an application that has a button (doesn't have to be a button) that when you hover over it a pop-up window appears. I have been able to print a message to the log when i hover over the button, but I can't figure out how to set the Hidden property of the image to NO. I tried giving the NSButtonCell (the class that receives the hover event) a delegate, but calling

[myButtonCell setDelegate:delegateObject]

Doesn't give the object a delegate. If I could find a way for the buttonCell and image to communicate (they are both in the same xib) or get the buttonCell to call a function in one of the classes which has it as an instance it would be an easy task to figure out the rest.

My explanation is a bit diffuse so I will try to explain better: I have a window object, with a view object, which has a subclass of a NSButtonCell object (IBOutlet). In the subclass of the NSButtonCell (lets call it MyButtonCell) I have a method that when call needs to inform the view, or the window, that the method has been called.

I feel like I have looked everywhere, but can't find the solution. I thought I would make it with a delegate, but I can't set a delegate for the buttonCell so I am stuck…

Edit:

Here is the code for the NSButtonCell and the Delegate: The delegate:

@interface MyView : NSView <MyButtonCellDelegate>
    {

    }
@property (assign) IBOutlet MyButtonCell *buttonCell1;
    - (void)toggleField:(int)fieldID;
@end

@implementation MyView

- (void)toggleField:(int)fieldID
{
    if (fieldID == 1) {
        [self.field1 setHidden:!buttonCell1.active];
    }
    NSLog(@"toggling");
}
@end

MyButtonCell:

@protocol MyButtonCellDelegate

- (void)toggleField:(int)fieldID;

@end

@interface MyButtonCell : NSButtonCell
{
    id <MyButtonCellDelegate> delegate;
}

@property BOOL active; //Used to lett the view know wether the mouse hovers over it
@property (nonatomic, assign) id  <DuErButtonCellDelegate> delegate;

-(void)_updateMouseTracking; //mouse tracking function, if you know a better way to do it that would be lovely

@end

@implementation MyButtonCell

@synthesize delegate;
@synthesize active;

- (void)mouseEntered:(NSEvent *)event
{
    active = YES;
    [[self delegate] toggleField:1];
    NSLog(@"entered");
}

- (void)mouseExited:(NSEvent *)event
{
    active = NO;
    [[self delegate] toggleField:1];
}

- (void)_updateMouseTracking {
    [super _updateMouseTracking];
    if ([self controlView] != nil && [[self controlView] respondsToSelector:@selector(_setMouseTrackingForCell:)]) {
        [[self controlView] performSelector:@selector(_setMouseTrackingForCell:) withObject:self];
    }


}

@end

Hope this is clear enough

Was it helpful?

Solution

I am not sure what you are really looking for, but if I understand what you are asking here:

I have a window object, with a view object, which has a subclass of a NSButtonCell object (IBOutlet). In the subclass of the NSButtonCell (lets call it MyButtonCell) I have a method that when call needs to inform the view, or the window, that the method has been called.

correctly, one possibility is for your NSButtonCell to post a NSNotification to the default notification center and have your view or window or whoever needs to know be an observer for that notification. You are free to define your own custom notifications.

Another possibility would be for your subclass of NSButtonCell to use:

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

and from your NSCell method that, when invoked, needs to inform it's view or window, can do:

[[self controlView] performSelectorOnMainThread:@selector( viewMethodToInvoke: ) withObject:anObject waitUntilDone:YES]

or

[[[self controlView] window] performSelectorOnMainThread:@selector( windowMethodToInvoke: ) withObject:anObject waitUntilDone:YES]

A third possibility is to do as you suggest and provide your NSButtonCell with an object that it can send a message to directly, but this is just the same thing as using performSelectorOnMainThread on the controlView or the controlView's window, but more work.

As for your mouse tracking code, I assume that you are using a NSTrackingArea. You can find documentation on them here: Using Tracking-Area Objects

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top