NSImageview在运行时添加不响应mousedown事件 [英] NSImageview adding at run time does not response to mousedown event

查看:1344
本文介绍了NSImageview在运行时添加不响应mousedown事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时创建了一个按钮和NSImageView控件。该按钮响应点击事件。但是imageview没有。任何建议?

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");

}

编辑使用 NSImageView ,因此对图像使用 NSButton 不是解决方案。

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

推荐答案

首先,你的代码有几个内存问题:当你使用 alloc / init ,然后将这些对象移动到将保留它们的其他对象,您需要 -release -autorelease 对象之后。

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 -addSubview:将视图插入视图层次结构,就像一个子视图数组。因此, -addSubview:保留您传入的视图,因此您需要使用 + alloc 。当调用 NSImageView setImage:时,它保留(或复制)您传递的映像,也可以使用 + alloc 来抵消创建。

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.

默认情况下, NSImageView 不会对 -mouseDown: -mouseUp: c $ c> NSControl 子类(即 NSButton )。如果它的工作在视觉上,它可能更有意义配置一个 NSButton 以一种方式,只是显示一个图像,而不是使用 NSImageView ,否则您可能需要创建 NSImageView 的自定义子类。

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.

c $ c> NSImageView 子类,我会认真考虑是否覆盖 mouseDown:是正确的事情,还是应该等到你收到 mouseUp:发送您的操作。例如,大多数按钮在点击鼠标时不立即发送他们的动作;相反,他们会等到你放开鼠标( mouseUp:),以防用户想改变主意。

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

这篇关于NSImageview在运行时添加不响应mousedown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆