从NSEvent块调用Cocoa ObjC方法

时间:2022-09-24 23:08:48

Why doesn't this code work:

为什么这个代码不能工作:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [NSEvent addGlobalMonitorForEventsMatchingMask:(NSScrollWheelMask) handler:^(struct NSEvent *event){
        [scrollEvent:event];
    }];
}

- (void)scrollEvent:event {
    NSLog( @"scroll" );
}

It says "'scrollEvent' undeclared".

它说“scrollEvent未申报”。

I'm just learning objc and cocoa, so I assume this is just a simple error.

我只是在学习objc和cocoa,所以我认为这只是一个简单的错误。

2 个解决方案

#1


5  

Your code appears to have some bugs in it. I've cleaned up some, please see below.

您的代码似乎有一些错误。我清理了一些,请看下面。

- (void) applicationDidFinishLaunching:(NSNotification *) aNotification { 
    [NSEvent addGlobalMonitorForEventsMatchingMask: NSScrollWheelMask handler:^(NSEvent *event){ 
        [self scrollEvent: event]; 
    }]; 
} 

- (void) scrollEvent: (NSEvent *) event { 
    NSLog( @"scroll" ); 
}

To summarize:

总结:

  • Your argument to the block should have been an NSEvent *, not a struct NSEvent *.
  • 你对block的参数应该是NSEvent *,而不是struct NSEvent *。
  • Your invocation of scrollEvent needed to be sent to self.
  • 您对scrollEvent的调用需要发送给self。
  • Your implementation of scrollEvent had an incorrect signature.
  • 您的scrollEvent的实现有一个错误的签名。

Hope that helps and good luck with the program.

希望能帮助并祝你好运。

#2


1  

I believe your :event parameter of the scrollEvent method needs to have a type (NSEvent*) for this to be a valid method signature.

我相信您的:scrollEvent方法的事件参数需要有一个类型(NSEvent*),这是一个有效的方法签名。

- (void)scrollEvent:(NSEvent*)event {
    NSLog( @"scroll" );
}

#1


5  

Your code appears to have some bugs in it. I've cleaned up some, please see below.

您的代码似乎有一些错误。我清理了一些,请看下面。

- (void) applicationDidFinishLaunching:(NSNotification *) aNotification { 
    [NSEvent addGlobalMonitorForEventsMatchingMask: NSScrollWheelMask handler:^(NSEvent *event){ 
        [self scrollEvent: event]; 
    }]; 
} 

- (void) scrollEvent: (NSEvent *) event { 
    NSLog( @"scroll" ); 
}

To summarize:

总结:

  • Your argument to the block should have been an NSEvent *, not a struct NSEvent *.
  • 你对block的参数应该是NSEvent *,而不是struct NSEvent *。
  • Your invocation of scrollEvent needed to be sent to self.
  • 您对scrollEvent的调用需要发送给self。
  • Your implementation of scrollEvent had an incorrect signature.
  • 您的scrollEvent的实现有一个错误的签名。

Hope that helps and good luck with the program.

希望能帮助并祝你好运。

#2


1  

I believe your :event parameter of the scrollEvent method needs to have a type (NSEvent*) for this to be a valid method signature.

我相信您的:scrollEvent方法的事件参数需要有一个类型(NSEvent*),这是一个有效的方法签名。

- (void)scrollEvent:(NSEvent*)event {
    NSLog( @"scroll" );
}