2011-05-09 156 views
0

MyNSImageView是NSImageView的一個子類,在這裏我有:鼠標事件處理程序

@interface MyNSImageView : NSImageView 
{ 
} 
@end 

@implementation MyNSImageView 

//- (void) mouseDown: (NSEvent *) theEvent 
//{ 
// do not wish to implement mouseDown event handler from here 
//} 
@end 

在另一類所謂的MainView,我有:

@interface MainView : NSView 
{ 
    MyNSImageView *ImageView1; 
    MyNSImageView *ImageView2; 

} 
@end 

- (void)awakeFromNib 
{ 
    ImageView1 = [[[MyNSImageView alloc] initWithFrame:NSMakeRect(5, 5, 240, 240)] autorelease]; 
    NSImage* image1 = [[[NSImage alloc] initWithContentsOfFile: @"/Volumes/MAC DAT2/pictures/MP6107.jpg"] autorelease]; 
    [ImageView1 setImage:image1]; 
    [self addSubview:ImageView1]; 

    ImageView2 = [[[MyNSImageView alloc] initWithFrame:NSMakeRect(300, 5, 240, 240)] autorelease]; 
    image1 = [[[NSImage alloc] initWithContentsOfFile: @"/Volumes/MAC DAT2/pictures/MP5784.jpg"] autorelease]; 
    [ImageView2 setImage:image1]; 
    [self addSubview:ImageView2]; 
} 

- (void) mouseDown2: (NSEvent *) theEvent 
{ 
    NSLog(@"mousedown2 from MainView"); 
} 
- (void) mouseDown1: (NSEvent *) theEvent 
{ 
    NSLog(@"mousedown1 from MainView"); 
} 
@end 

- (void) mouseDown: (NSEvent *) theEvent 
{ 
    NSLog(@"mousedown from MainView"); 
} 

在的MainView,當我點擊ImageView1或ImageView2,我想有mouseDown1或mouseDown2方法來處理事件相應不是mouseDown方法。

我已閱讀關於目標/動作/委託和響應者的東西,但仍然看不到確切的語法來做到這一點。

回答

2

一種方法是使用一個委託:

首先聲明一個委託協議,爲您NSImageView子類:

@class MyNSImageView; 
@protocol MyNSImageViewDelegate <NSObject> 

- (void)myImageView:(MyNSImageView *)view mouseDown:(NSEvent *)event; 

@end 


@interface MyNSImageView : NSImageView { 

} 
// declare the delegate member 
@property (assign) id<MyNSImageViewDelegate> delegate; 
@end 

@implementation MyNSImageView 
@synthesize delegate = _delegate; 

// In your mouseDown method, notify the delegate 
- (void)mouseDown:(NSEvent *)event { 
    if([self.delegate respondsToSelector:@selector(myImageView:mouseDown:)]) { 
     [self.delegate myImageView:self mouseDown:event]; 
    } 
} 

@end 

然後,聲明你MainView類來實現MyNSImageViewDelegate協議:

@interface MainView : NSView <MyNSImageViewDelegate> { 
    MyNSImageView *imageView1; 
    MyNSImageView *imageView2; 
} 
@end 

並在您的MainView執行:

- (void)awakeFromNib { 
    // create your image views then add our instance as the delegate to each 
    ImageView1.delegate = self; 
    ImageView2.delegate = self; 
} 

// here we implement the `MyNSImageViewDelegate` method, which will get 
// called when any `MyImageNSView` instance we've added ourselves as 
// delegate to gets clicked. 
- (void)myImageView:(MyNSImageView *)view mouseDown:(NSEvent *)event { 
    if (view == imageView1) { 
     NSLog(@"imageView1 clicked");  
    } else if (view == imageView2) { 
     NSLog(@"imageView2 clicked");  
    } 
} 
+0

這樣做。我知道這將涉及選擇器和委託的東西。但是不能把所有的連接聯繫在一起。做得好!!!如果你熟悉C#或C++,他們處理這種情況要簡單得多。 (順便說一句:你的代碼中缺少一行是「@synthesize delegate;」,它需要在MyNSImageView的實現文件中。) 只是一個側面的問題:以這種方式處理鼠標事件到MVC模型? – user523234 2011-05-11 14:26:09

+0

感謝您指出缺少的@synthesize ...抱歉。 – 2011-05-11 14:33:30

+0

檢查我的編輯是否有其他有效的方法......目標動作機制可能是這裏最常用的Cocoa/MVC方法。 – 2011-05-11 14:47:04

1

您應該閱讀約​​。對於要調用的MyCallingClass的-mouseDown:方法,該類的一個實例必須位於當前的響應者鏈中,並且鏈中的其他響應者不應該處理該事件。處理這種

+0

Caleb:謝謝你的迴應。我原來的問題不清楚。我對這個問題做了一些改變。如果mouseDown事件處理程序未在MyNSImageView中實現,則來自MainView(MyCallingClass)的mouseDown事件處理程序將接管。但我需要能夠區分mousedown事件從不同的圖像。任何建議將不勝感激。 – user523234 2011-05-11 12:09:22