2012-12-19 29 views
6

我在我的Mac应用程序中有一些NSImageView,用户可以拖放像.png或.pdf这样的对象,将它们存储到用户共享默认值中,工作正常。NSImageView双击动作

我现在想为用户双击这些NSImageView时设置一个动作,但它似乎有点困难(我对NSTableView没有任何问题,但'setDoubleAction'不可用于NSImage,并且吨答案(点击这里或与谷歌)关于NSImageView的行动指向制作NSButton代替NSImageView,这样就不会帮助)

这里是我的AppDelegate.h的一部分:

@interface AppDelegate : NSObject <NSApplicationDelegate>{ 

    (...) 

    @property (assign) IBOutlet NSImageView *iconeStatus; 

    (...) 

@end 

,这里是我的AppDelegate.m的一部分:

#import "AppDelegate.h" 

@implementation AppDelegate 

(...) 

@synthesize iconeStatus = _iconeStatus; 

(...) 

- (void)awakeFromNib { 

    (...) 

[_iconeStatus setTarget:self]; 
[_iconeStatus setAction:@selector(doubleClick:)]; 

    (...) 

} 

(...) 

- (void)doubleClick:(id)object { 
     //make sound if that works ... 
     [[NSSound soundNamed:@"Basso"] play]; 

} 

但这并不奏效。

任何人都可以告诉我什么是最简单的方法吗?

回答

8

您需要继承NSImageView并添加下面的方法来子类的实现:

- (void)mouseDown:(NSEvent *)theEvent 
{ 
    NSInteger clickCount = [theEvent clickCount]; 

    if (clickCount > 1) { 
     // User at least double clicked in image view 
    } 
}