如何从Objective-C Cocoa接口使用Objective-C ++调用C ++方法 [英] How to call C++ method from Objective-C Cocoa Interface using Objective-C++

查看:128
本文介绍了如何从Objective-C Cocoa接口使用Objective-C ++调用C ++方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的OS X 10.9项目在Objective-C中设置了GUI,并在C ++类中进行了所有的处理。这似乎不是最好的方式,但这是给我,我必须处理这些约束。我目前在Cocoa GUI中有一个 NSSlider 。我想能够让这个 NSSlider 在C ++类中控制一个变量 x

Right now my OS X 10.9 project is set up with a GUI in Objective-C and all the processing in a C++ class. This doesn't seem like the best way, but this was given to me and I have to work with these constraints. I currently have a NSSlider in the Cocoa GUI. I want to be able to have this NSSlider control a variable x in the C++ class.

目前,当我运行程序时,公差栏正在工作(工作意味着它会根据我的滑动不断更新其值)。

Currently the tolerance bar is working when I run the program (working meaning it's continuously updating its value as per my sliding).

NSSlider Slider.h ):

@interface Slider : NSObject {
@private
    __weak IBOutlet NSTextField *sliderValue;
    __weak IBOutlet NSSlider *slider;
}

- (IBAction)updateSliderValue:(id)sender;

@end

NSSlider Slider.mm ):

#import "Slider.h"      // The NSSlider object
#import "CMain.h"       // The C++ class that does all the processing (not sure if this is the way to include a C++ interface in an Objective-C++ class or if this will allow me to call its methods)

@implementation Slider

-(void) awakeFromNib {
    [slider setIntValue:40];
    [sliderValue setIntValue:[slider intValue]];
}

- (IBAction)updateSliderValue:(id)sender {
    [sliderValue setIntValue:[slider intValue]];
    // Here is where I'd think that if I have a setValueofX(int number) method in the C++ class
    // I could call this somehow using objective-C++. I don't know if I can or how to call this method here
}

是来自 Main.h 的相关片段:

class CMain(){
    public:
        CMain();
        void setValueofX(int number);
        int getValueofX();

    private:
        int x;
};

如何将 CMain.h Objective-C ++ (其中, .h 文件或 .mm file?and how?),这样它就允许我调用 CMain.h 中的方法?我如何使用Objective-C中的方法调用来使用 setValueofX(sliderValue)设置 x

How do I include CMain.h in the Objective-C++ (which, the .h file or .mm file? and how?) such that it would allow me to call the methods in CMain.h? How would I phrase the method call in Objective-C to set x's value using the setValueofX(sliderValue)?

如果需要更多信息,请告诉我们!

Please let me know if more information is needed! Any help or thoughts are appreciated!

谢谢!

推荐答案

找到一个解决方案,注意到有一个现有的包装类 CWrapper.h ,它是一个目标C ++实现类 CWrapper.mm 。有一个 CMain 变量实例化为 cmain 。我为 x 创建了一个getter方法,我在封装器类中创建了一个静态方法 +(void)passX:(int)number; code>,我在滑块类中调用。我选择了一个静态方法,因为对于这个应用程序,这个值永远不会有不同的对象之间。我希望我在这里做出正确的选择!

I found a solution by noting that there was an existing wrapper class CWrapper.h and it was an objective-C++ implementation class CWrapper.mm. There was a CMain variable instantiated as cmain. I made a getter method for x and I simply created a static method in the wrapper class + (void) passX:(int) number; that I called in the slider class. I chose a static method because this for this application, this value will never have to be different between objects. I hope I made the right choice here!

查看以下代码更改

See code changes below:

$ c> Slider.h 文件。

I added this to the Slider.h file.

- (int)X;

我添加了getter和 [CWrapper passX:[self getX]];

I added the getter and [CWrapper passX:[self getX]]; to the updateSliderValue method in the Slider.mm file.

- (int)X {
    return [slider intValue];
}

- (IBAction)updateSliderValue:(id)sender {
    [sliderValue setIntValue:[slider intValue]];
    [CWrapper passX:[self getX]];
}

这是我添加到CWrapper.h的代码。

This is the code I added to CWrapper.h.

+ (void) passX:(int) number;

这是我添加到CWrapper.mm的代码。

This is the code I added to CWrapper.mm.

+ (void) passX:(int)num
{
    cmain.setValueofX(num);
}

这篇关于如何从Objective-C Cocoa接口使用Objective-C ++调用C ++方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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