2011-05-08 30 views
1

我在運行時創建了一個按鈕和一個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與圖像是不是一個解決方案。

+0

什麼類的代碼嗎? – 2011-05-08 04:35:16

+0

NSObject的一個子類。不過,請參閱我對NSGod的評論以澄清我的問題。 – user523234 2011-05-09 03:43:39

回答

8

首先,你的代碼中有幾個內存問題:當您使用alloc/init本地對象,然後用手這些對象關閉,以將它們保留其他對象,你需要或者-release-autorelease的事後對象。

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自動釋放它以抵制創建。當您撥打NSImageViewsetImage:時,它會保留(或複製)您傳入的圖像,因此您需要自動釋放該圖像以抵制使用+alloc的創建。

默認情況下,NSImageView沒有反應到-mouseDown:,或-mouseUp:像其他NSControl子類(即NSButton)做的。如果它在視覺上工作,那麼配置NSButton可能更有意義,它只是簡單地顯示圖像而不是使用NSImageView,否則您可能需要創建NSImageView的自定義子類。

NSImageView子類中,我會認真考慮重寫mouseDown:是否是正確的做法,或者是否應該等到收到mouseUp:才能發送您的操作。例如,大多數按鈕在點擊鼠標時不會立即發送它們的動作;相反,他們一直等到你放開鼠標(mouseUp:),以防用戶想改變主意。

在任何情況下,子類會是什麼樣子:

@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

你對需要的子類NSImageView是正確的。考慮到這一點,你如何在調用模塊(類)中實現mouseUp事件 - 而不是從MDImageView?基本上,我需要將我的鼠標事件處理程序方法駐留在調用類中。 (你可以通過委託在C++或C#中做類似的事情。)有關內存管理的很好的解釋! – user523234 2011-05-09 03:37:07

相關問題