鼠标事件处理程序

发布于 2024-11-05 15:00:05 字数 1441 浏览 3 评论 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 方法来相应地处理事件,而不是鼠标按下方法。

我已经阅读了有关目标/操作/委托和响应者的内容,但仍然看不到执行此操作的确切语法。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋心╮凉 2024-11-12 15:00:05

处理此问题的一种方法是使用委托:

首先,为您的 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

协议:

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

然后,声明您的 MainView 类来实现MyNSImageViewDelegate 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");    
    }
}

One way to handle this is with a delegate:

First you declare a delegate protocol for your NSImageView subclass:

@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

Then, declare your MainView class to implement the MyNSImageViewDelegate protocol:

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

And in your MainView implementation:

- (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");    
    }
}
爱的十字路口 2024-11-12 15:00:05

您应该阅读响应者链 。对于要调用的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文