鼠标事件处理程序
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 方法来相应地处理事件,而不是鼠标按下方法。
我已经阅读了有关目标/操作/委托和响应者的内容,但仍然看不到执行此操作的确切语法。
MyNSImageView is a subclass of NSImageView, here I have:
@interface MyNSImageView : NSImageView
{
}
@end
@implementation MyNSImageView
//- (void) mouseDown: (NSEvent *) theEvent
//{
// do not wish to implement mouseDown event handler from here
//}
@end
In another class called MainView, I have:
@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");
}
In the MainView, when I click on the ImageView1 or ImageView2, I would like to have the mouseDown1 or mouseDown2 method to handle the event accordingly not the mouseDown method.
I have read about target/action/delegate and responder stuff, but still could not see the exact syntax to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理此问题的一种方法是使用委托:
首先,为您的
NSImageView
子类声明一个委托协议:协议:
然后,声明您的
MainView
类来实现MyNSImageViewDelegateMainView
实现:One way to handle this is with a delegate:
First you declare a delegate protocol for your
NSImageView
subclass:Then, declare your
MainView
class to implement the MyNSImageViewDelegate protocol:And in your
MainView
implementation:您应该阅读响应者链 。对于要调用的 MyCallingClass 的 -mouseDown: 方法,该类的实例必须位于当前响应者链中,并且链下游的任何其他响应者都不应处理该事件。
You should read about the responder chain. For MyCallingClass's -mouseDown: method to be called, an instance of that class has to be in the current responder chain, and no other responder further down the chain should handle that event.