Objective-C中的鼠标向下事件 [英] Mouse Down events in Objective-C

查看:229
本文介绍了Objective-C中的鼠标向下事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题以前被问过很多,但没有什么能为我工作。以下代码根本不会做任何事情。

   - (void)mouseDown:(NSEvent *)event {
NSLog(@It worked!);

}



我尝试了很多不同的方法工作,包括以这种方式创建自定义 NSEvents

  NSEvent * someEvent ; 

- (void)mouseDown:(NSEvent *)someEvent {
NSLog(@It worked!

}

这是我的.h文件:

  @interface test:NSWindow< NSWindowDelegate> {

}

有人会解释如何做这件事吗? / p>

解决方案

确保您的类继承自 NSWindow ,并符合< NSWindowDelegate> 协议。否则,这只是一个名为 mouseDown 的方法,没有人会调用它。



更新:更改您的头文件,如下所示:

  @interface test:NSWindow< NSWindowDelegate> ; {

}

换句话说, / em>在接口定义或 .h 文件中的任何地方放置一个 mouseDown 的原型。 p>

在您的实现文件中( .m )只需要方法:

   - (void)mouseDown:(NSEvent *)someEvent {
NSLog(@It worked!
}

假设您在设备中启用了日志记录阅读 NSLog 从程序的其他地方输出?),你应该看到它的工作!



我不是任何方式的obj-C专家,但我认为通过将 mouseDown prototype里面的接口定义,你基本上创建自己的自定义 mouseDown 方法隐藏真正的一个。这表明编译器在窗口点击时不应该调用 mouseDown 方法。


I know this question has been asked a lot before, but nothing will work for me. The following code will not do anything at all.

- (void) mouseDown:(NSEvent*)event {
    NSLog(@"It worked!");

}

I have tried a lot of different methods to get this to work, including creating custom NSEvents in this way:

NSEvent *someEvent;

- (void) mouseDown:(NSEvent*)someEvent {
    NSLog(@"It worked!");

}

This is my .h file:

@interface test : NSWindow <NSWindowDelegate> {

}

Would somebody explain how to make this do something?

解决方案

Make sure your class inherits from NSWindow and conforms to the <NSWindowDelegate> protocol. Otherwise, that's just a method that happens to be named mouseDown, and nobody will ever call it.

Update: Change your header file so that it looks like this:

@interface test : NSWindow <NSWindowDelegate> {  

} 

In other words, don't put a prototype of mouseDown inside the interface definition, or anywhere else in the .h file.

In your implementation file (.m) put just the method:

- (void) mouseDown:(NSEvent*)someEvent {         
    NSLog(@"It worked!");          
} 

Assuming that you have logging turned on in the device (are you sure you can read NSLog output from elsewhere in your program?), you should see "It worked!" printed there.

I'm not an obj-C expert by any means, but I think by putting the mouseDown prototype inside the interface definition, you were basically creating your own custom mouseDown method which hid the "real" one. This indicated to the compiler that it should not call your mouseDown method on a window click.

这篇关于Objective-C中的鼠标向下事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆