1

I've been searching for answers to this question and while I have done the steps I still cannot seem to get the mouseDown method to be called. What I want is when the user clicks and holds down on the image, it should print out "Mouse down!". (Eventually what I want to do is the user to click and hold on the image to cause a sound to play, and when they let go the sound will stop).

Here's what I have. My header:

@interface MyNSImageView : NSImageView <NSImageDelegate> {
}
@end

And my class:

@implementation MyNSImageView

- (void)mouseDown:(NSEvent *)event {

     if ([[self target] respondsToSelector:[self action]]) {
     [NSApp sendAction:[self action] to:[self target] from:self];
     }
    NSLog(@"Mouse Down!");
}

@end

Does this look right? If so, then are there other problems which might be interfering with it? Of not, what should I do?

Thanks heaps!

Moonlight293
  • 197
  • 1
  • 5
  • 13

2 Answers2

4

Here's my solution:

Add an invisible button without border or title above the NSImageView.

:)

Nix Wang
  • 814
  • 10
  • 18
3

First thought: Try implementing acceptsFirstResponder and return yes. Then have your delegate set the first responder to the image view....

UPDATE:

So, I just created a new project, and added a class MyClass to it, which inherits from NSImageView. mouseDown is implemented:

-(void)mouseDown:(NSEvent *)theEvent
{
NSLog(@"Mouse Down");
}

and initWithFrame is implemented:

- (id)initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    if (self) {
        NSURL *newURL = [[NSBundle mainBundle] URLForImageResource:@"ARandomImageIAddedtoProject"];
        NSImage *newImage = [[NSImage alloc] initWithContentsOfURL:newURL];
        [self setImage:newImage];
    }

return self;
}

To make the view visible.

I then edited the nib file by adding a custom class to the window, and changing the class of the custom class to MyClass. It worked fine, so I dunno whats wrong.

Daniel
  • 1,079
  • 1
  • 11
  • 25
  • I have just done so, but still nothing is being printed out. I have made two buttons - would that interfere with the initFirstResponder? (even though I only have one as the first responder) – Moonlight293 Aug 10 '11 at 00:41
  • Hah - I may have figured out why. My xib file isn't updating when I change it. I'm using Xcode 4 - got any ideas as to why it's not working? – Moonlight293 Aug 10 '11 at 03:59
  • I dunno - Are you saving the xib file after editing it? Do you have permission to edit the file? Other than that I dunno whats wrong. I'm updating my answer with my test.. – Daniel Aug 10 '11 at 23:14
  • 1
    Solved - I was editing the wrong xib file... -headdesk- Thanks heaps. – Moonlight293 Aug 12 '11 at 05:19