现在我的OS X 10.9项目在Objective-C中用GUI设置,而在C++类中进行所有处理。这看起来并不是最好的方式,但这是给我的,我必须处理这些限制。我目前在Cocoa GUI中有NSSlider
。我希望能够在C++类中控制变量x
。如何使用Objective-C++从Objective-C Cocoa接口调用C++方法
当我运行该程序时,容差条正在工作(工作意味着它不断更新其滑动值)。
报头为: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
文件?以及如何?),这样可以让我调用CMain.h
中的方法?如何在Objective-C中使用setValueofX(sliderValue)
来短语方法调用x
的值?
如果需要更多信息,请让我知道!任何帮助或想法表示赞赏!
谢谢!
只需要注意一点:不要在Objective-C中用“get”启动getter的名字。这是保留给参数副作用的方法。把它称为 - 宽容。这样,您就可以使用KVO或其他依赖于您遵守此类命名标准的机制。 – uliwitness 2014-10-31 15:50:21
@uliwitness感谢您的提示! – 2014-11-01 20:41:10