开发者

objective c++ accessing an NSSlider from c++ callback

AppController.h

@interface AppController : NSObject {

}

@property (retain) IBOutlet NSSlider * mySlider;

void setMySlider (NSSlider *ns);// c function

AppController.m

@implementation AppController

int myAmount=0;
@synthesize mySlider;


void setMySlider(NSSlider *mySlider){

    [ns setIntValue:10]; //ok
    ns.intValue =myAmount;//ok
}

myCallBackFunction(double delta,std::vector<unsigned char>*myData,void *userData){

m开发者_StackOverflow中文版yAmount=50;
 NSLog(@"%i", myAmount); // ok does display value of myAmount

// should move my slider the value of my amount by calling the following C function:

changeSliderVelocity(mySlider);// error message mySlider was not declared in this scope

}


No.

In this case, even though myCallBackFunction() is placed between @implementation and @end it is not within the class scope of AppController. The int myAmount is like a static Class-ish Member (I have actually no idea what its scope is. Global??) because you defined it in the Implementation file, it is not actually part of the object AppController.

No guarantees this will work, but give it a shot. It assumes there will only ever be 1 (one) instance of AppController. Read up more on Singletons in Obj-C/C++.

@implementation AppController

static AppController *sharedAppInstance = nil;

- (void) init
{
  // ... whatever other code you have
  if (sharedAppInstance == nil)

     sharedAppController = self; //error message sharedAppController was not declared in //this scope. So I replaced the line sharedAppController = self; by:

       sharedAppInstance= self;

}

myCallBackFunction(double delta,std::vector<unsigned char>*myData,void *userData){

changeSliderVelocity([[AppController sharedAppInstance] mySlider]); // Replace this line with to make it work:
changeSliderVelocity([sharedAppInstance mySlider]);//ok does work.

}

@end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜