我在運行時創建了一個按鈕和一個NSImageView控件。該按鈕響應了點擊事件。但imageview沒有。有什麼建議麼?在運行時添加NSImageview不會響應mousedown事件
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
與圖像是不是一個解決方案。
什麼類的代碼嗎? – 2011-05-08 04:35:16
NSObject的一個子類。不過,請參閱我對NSGod的評論以澄清我的問題。 – user523234 2011-05-09 03:43:39