Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4607370
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:41:37+00:00 2026-05-22T00:41:37+00:00

I created a button and a NSImageView controls at run time. The button responded

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:41:38+00:00Added an answer on May 22, 2026 at 12:41 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attaching onclick event on dynamically created button but the event is not
Actually I created a button using the cobogw widget library. But the button does
I created a button and added an action for it, but as soon as
Created a button in my aspx file and add its click method in the
I created a Button in MXMXL. On button click, I create a Menu as
I have created button without any border using LWUIT resource editor. I am able
I created a Button,While i Click that Button a new Dialog Box is Displayed.In
I have created a button. Now I want to do: A single click of
So I created a button in xaml (made a rectangle > right click >
I am trying to extend dijit.form.Button with an extra attribute but this is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.