1

I created a button and a NSImageView controls at run time. The button responded to the click event. But the imageview does not. Any suggestions?

    NSView *superview = [((MyAppAppDelegate *)[NSApp delegate]).window contentView];

    NSButton *button = [ [ NSButton alloc ] initWithFrame: NSMakeRect(300, 50, 50.0, 50.0 ) ];
    [superview addSubview:button];
    [button setTarget:self];
    [button setAction:@selector(button_Clicked:)];

    NSImageView *myImageView = [[NSImageView alloc] initWithFrame:NSMakeRect(5, 5, 240, 240)];
    NSString* filePath = @"/Volumes/MAC DAT2/pictures/TVX1/153/MP6107frame5786.jpg";
    NSImage* image1 = [[NSImage alloc] initWithContentsOfFile:filePath];
    [myImageView setImage:image1];
    [superview addSubview:myImageView];
    [myImageView setTarget:self];
    [myImageView setAction:@selector(mouseDown:)];

}
- (IBAction)button_Clicked:(id)sender
{
    NSLog(@"button clicked");
}
-(void) mouseDown:(NSEvent *)event
//- (IBAction)mouseDown:(NSEvent *)event  //also have tried this one.
{
    NSLog(@"mousedown");

}

Edit: I need to use an NSImageView, so using an NSButton with an image is not a solution.

user523234
  • 14,323
  • 10
  • 62
  • 102

1 Answers1

8

First of all, your code has several memory issues: when you create local objects using alloc/init, and then hand those objects off to other objects that will retain them, you need to either -release or -autorelease the objects afterward.

NSView *superview = [((MyAppAppDelegate *)[NSApp delegate]).window contentView];

// memory leak averted:
NSButton *button = [[[NSButton alloc] initWithFrame:
                      NSMakeRect(300, 50, 50.0, 50.0 )] autorelease];

[superview addSubview:button];
[button setTarget:self];
[button setAction:@selector(button_Clicked:)];

// memory leak averted:
NSImageView *myImageView = [[[NSImageView alloc] initWithFrame:
                              NSMakeRect(5, 5, 240, 240)] autorelease];

NSString* filePath = @"/Volumes/MAC DAT2/pictures/TVX1/153/MP6107frame5786.jpg";

// memory leak averted:
NSImage* image1 = [[[NSImage alloc] initWithContentsOfFile:filePath] autorelease];

[myImageView setImage:image1];
[superview addSubview:myImageView];
[myImageView setTarget:self];
[myImageView setAction:@selector(mouseDown:)];

NSView's -addSubview: inserts the view into the view hierarchy, which is like an array of subviews. As a result, -addSubview: retains the view that you pass in, so you need to autorelease it to counteract your creation using +alloc. When you call NSImageView's setImage:, it retains (or copies) the image you pass in, so you need to autorelease that also to counteract the creation using +alloc.

By default, NSImageView doesn't react to -mouseDown:, or -mouseUp: like other NSControl subclasses (namely NSButton) do. If it works visually, it might make more sense to configure an NSButton in a way that simply shows an image rather than using an NSImageView, otherwise you'd likely need to create a custom subclass of NSImageView.

In the NSImageView subclass, I would seriously contemplate whether overriding mouseDown: is the proper thing to do, or whether you should wait until you receive mouseUp: to send your action. For example, most buttons do not immediately send their action upon clicking the mouse down; rather, they wait until you let go of the mouse (mouseUp:), in case the user wants to change their mind.

In any case, the subclass would look like:

@interface MDImageView : NSImageView {

}

@end

@implementation MDImageView

- (void)mouseUp:(NSEvent *)event {
      if ([[self target] respondsToSelector:[self action]]) {
         [NSApp sendAction:[self action] to:[self target] from:self];
      }
}

@end
NSGod
  • 22,699
  • 3
  • 58
  • 66
  • You were right on the needing subclass the NSImageView. With that in mind, how do you implement the mouseUp event in the calling module (class) - not from MDImageView? Basically, I need my mouse event handler methods to be resided inside the calling class. (You can do that similar thing in C++ or C# through delegation.) Good explanation on the memory management! – user523234 May 09 '11 at 03:37