I subclassed NSButton
so that the button image would change when it is clicked. The following code is used for the subclass.
#import "MyImageButton2.h"
@interface MyImageButton2 ()
@property (strong, nonatomic) NSCursor *cursor;
@end
@implementation MyImageButton2
- (void)setImage:(NSImage *)image {
[super setImage:image];
if (!self.image) {
self.image = [NSImage imageNamed:@"buttonicon"];
}
}
- (void)mouseDown:(NSEvent *)theEvent {
[super mouseDown:theEvent];
self.image = [NSImage imageNamed:@"buttonicon2"];
}
- (void)mouseUp:(NSEvent *)theEvent {
[super mouseUp:theEvent];
self.image = [NSImage imageNamed:@"buttonicon"];
}
- (void)resetCursorRects {
NSCursor *cursor = (self.cursor) ? self.cursor : [NSCursor pointingHandCursor];
if (cursor) {
[self addCursorRect:[self bounds] cursor:cursor];
} else {
[super resetCursorRects];
}
}
@end
And here is the current button. As you can see, the button does not return to its original image after it is clicked, the mouseUp
event.
Any ideas on why the image is not returned to its original state during the mouseUp
event?