一个 obj-c 函数在 C 回调中工作正常,而另一个在调用时却无法正常工作
我有一个应用程序,它依赖于 C 回调方法从 MacBook 的触控板检索多点触控信息。以下是我如何向用户传达此数据的截断版本:
在 MT2AppDelegate.h 中:
#import "MyView.h"
@interface MT2AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSTextField *L1;
IBOutlet MyView *MTView;
}
@property (retain, nonatomic) IBOutlet MyView *MTView;
@property (retain, nonatomic) IBOutlet NSTextField *L1;
在 MT2AppDelegate.m 中:
#import "etc. etc."
static MT2AppDelegate *sharedWithCAppDelegateReference;
@implementation MT2AppDelegate
@synthesize L1, MTView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
sharedWithCAppDelegateReference=self;
[L1 setStringValue:@"Hello"];//this works
[MTView drawRect2];//this also works
MTDeviceRef dev = MTDeviceCreateDefault();
MTRegisterContactFrameCallback(dev, callback);
MTDeviceStart(dev, 0);
}
int callback( int example, float etc) {
[sharedWithCAppDelegateReference->L1 setStringValue:@"hello"];//this works
[sharedWithCAppDelegateReference->MTView drawRect2];//this does NOT work
return 0;
}
因此,出于某种原因,在一个 IBOutlet 上使用 C 函数可以正常工作,而另一个 IBOutlet 则不执行任何操作。相同的两个函数都可以在 Objective-c 中工作。我似乎也无法从 C 内部的应用程序委托调用函数。例如,[sharedWithCAppDelegateReference setPoint];
不执行任何操作。
另外,如果有更优雅的方法来做到这一点(我确信有),我洗耳恭听。这里发生的事情是,这个回调函数不断更新,报告有关触控板上手指位置的信息。我想对视图进行动画处理,以便它能够直观地表示手指位置。 FingerMgmt 应用程序在这方面做得非常出色,但没有可用的源代码。
我目前有我的应用程序委托,其中包含回调函数,以及另一个类(“MyView”),它是我的应用程序窗口中视图的类。应用程序委托通过界面生成器中的 IBOutlet 链接到视图,我计划向其传递命令以更新代表手指的椭圆形的位置。但同样,这对我不起作用。
I have an application that relies on a C callback method to retrieve multitouch information from a macbook's trackpad. Here's the truncated version of how I communicate this data to the user:
in MT2AppDelegate.h:
#import "MyView.h"
@interface MT2AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSTextField *L1;
IBOutlet MyView *MTView;
}
@property (retain, nonatomic) IBOutlet MyView *MTView;
@property (retain, nonatomic) IBOutlet NSTextField *L1;
in MT2AppDelegate.m:
#import "etc. etc."
static MT2AppDelegate *sharedWithCAppDelegateReference;
@implementation MT2AppDelegate
@synthesize L1, MTView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
sharedWithCAppDelegateReference=self;
[L1 setStringValue:@"Hello"];//this works
[MTView drawRect2];//this also works
MTDeviceRef dev = MTDeviceCreateDefault();
MTRegisterContactFrameCallback(dev, callback);
MTDeviceStart(dev, 0);
}
int callback( int example, float etc) {
[sharedWithCAppDelegateReference->L1 setStringValue:@"hello"];//this works
[sharedWithCAppDelegateReference->MTView drawRect2];//this does NOT work
return 0;
}
So, for some reason, using a function on one IBOutlet works from C just fine, while another one does nothing. The same two functions both work in objective-c. I also cannot seem to call a function from the app delegate inside C. For example, [sharedWithCAppDelegateReference setPoint];
does nothing.
Also, if there is a more elegant way to do this (I'm sure there is), I'm all ears. What is happening here is that this callback function is constantly updating, reporting the info about finger positions on the trackpad. I want to animate a view so that it gives a visual representation of finger location. The app FingerMgmt does a perfect job of this, but no source code is available.
I currently have my app delegate with the callback function inside it, and another class ("MyView") that is the class of the view in my application's window. The app delegate is linked to the view via an IBOutlet in interface builder, and I plan to pass commands to it to update the location of the ovals representing fingers. But again, this is not working for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用
->
;完全不是标准的 Obj-C 氛围。此外,实例变量和属性应以小写字母开头。根本问题是回调 API 没有值得上下文的指针。大多数回调 API 的定义如下:
在哪里设置它们,例如:
并且,当调用回调时,上下文指针指向您传递到
set_up_callback()
函数中的任何内容。通常,您将传递 self 或其他一些对象作为上下文(当然,首先保留),然后您可以像任何其他对象一样对其进行类型转换和调用方法。您的回调不会执行此操作,因此您需要全局变量,并且您的解决方案几乎与预期行为一致。保存以供
->
使用。不要那样做。相反,我会定义一个类似
-handleMTCallbackWithExample:(int)anEx etc:(float)anEtc;
的方法,然后将其作为我的callback()
中的一行代码调用代码> 功能。Don't use
->
; totally not the standard Obj-C vibe. Also, instance variables and properties should start with lowercase letters.The underlying problem is that the callback API doesn't have a pointers worth of context. Most callback APIs are defined like:
Where you set 'em up something like:
And, when the callback is called, the context pointer points to whatever thing you passed into the
set_up_callback()
function. Typically, you would passself
or some other object as your context (retained first, of course) and then you could typecast and call methods on it like any other object.Your callback doesn't do that and, thus, you need the global variable and your solution is pretty much inline with the expected behavior. Save for the use of
->
. Don't do that.Instead, I'd define a method like
-handleMTCallbackWithExample:(int)anEx etc:(float)anEtc;
and then call that as the one line of code in mycallback()
func.