現在我的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