鼠标事件:哪个实例是哪个? (目标C) [英] Mouse event: which instance is which? (Objective-C)

查看:72
本文介绍了鼠标事件:哪个实例是哪个? (目标C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Objective-C,并且需要处理鼠标按下事件.

我的标题是这样的:

@interface TestAppDelegate : NSWindow <NSWindowDelegate> {
  NSWindow *window;
  IBOutlet NSTextField* text_box;
}

实施文件为:

-(void)mouseDown:(NSEvent *)event {
  [text_box setFloatValue:1.0];
  NSLog(@"It worked!");
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
  [text_box setFloatValue:-1.0];
}

,该界面只有一个文本字段连接到我的text_box.在xib文件中,将类名称设置为TestAppDelegate.

现在,鼠标按下事件似乎可以正常工作,正如我所看到的成功!"日志.但是,文本框不会更新,并且保持为-1.

要了解发生了什么,我添加了一个链接到的按钮

-(IBAction)scrollerChanged:(id)sender {
  [text_box setFloatValue:2.0];
}

,这可以将我的text_form正确更新为2.因此,我的解释是,鼠标按下事件正在调用TestAppDelegate的另一个实例.我对吗?我在这里做错了什么吗?

谢谢!

解决方案

看起来确实有多个TestAppDelegate实例. (同时具有NSWindow对象也是它自己的委托应用委托,这有点不寻常.)

仅凭猜测,您可能在xib中有一个将类设置为TestAppDelegateNSWindow,而在xib中又有一个通用的NSObject对象也已设置为您的TestAppDelegate类. /p>

您可能已将text_box链接到您的一个实例(可能是应用程序委托NSObject之一),而您的NSWindow实例是实际接收mouseDown:事件的实例.

I am learning objective-C, and I need to handle a mouse down event.

My header is something like:

@interface TestAppDelegate : NSWindow <NSWindowDelegate> {
  NSWindow *window;
  IBOutlet NSTextField* text_box;
}

while the implementation file is:

-(void)mouseDown:(NSEvent *)event {
  [text_box setFloatValue:1.0];
  NSLog(@"It worked!");
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
  [text_box setFloatValue:-1.0];
}

and the interface has only an text field connected to my text_box. In the xib file I set the class name to TestAppDelegate.

Now, the mouse down event seems to work, as I can see the "It worked!" log. However, the text box is not updated, and stays -1.

To understand what is happening, I added a button which is linked to

-(IBAction)scrollerChanged:(id)sender {
  [text_box setFloatValue:2.0];
}

and this correctly updates my text_form to 2. Hence, my interpretations is that the mouse down event is calling a different instance of TestAppDelegate. Am I right? Am I doing something fundamentally wrong here?

Thanks!

解决方案

It does look like you’ve actually got multiple instances of TestAppDelegate. (It’s a little unusual to have an NSWindow object also be it’s own delegate and the app delegate).

Just guessing, you might have an NSWindow in your xib that you’ve set the class to TestAppDelegate, and another generic NSObject object in the xib that also is set to your TestAppDelegate class.

You’ve probably linked text_box to just one of your instances (probably the app delegate NSObject one), while your NSWindow instance is the one that actually receives the mouseDown: event.

这篇关于鼠标事件:哪个实例是哪个? (目标C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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