如何从c ++类调用Objective C实例方法?在TestApp.cpp中,我想在TestDelegate.mm中调用updateUI
TestDelegate.h
#include "cinder/app/CinderView.h"
#include "TestApp.h"
#import <Cocoa/Cocoa.h>
@interface TestDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet CinderView *cinderView;
IBOutlet NSWindow *window;
TestApp *mApp;
}
@property (assign) IBOutlet NSWindow *window;
- (IBAction)subdivisionSliderChanged:(id)sender;
- (void)updateUI;
@end
TestDelegate.mm
#include "cinder/Cinder.h"
#import "TestDelegate.h"
@implementation TestDelegate
@synthesize window;
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
mApp = new TestApp;
mApp->prepareLaunch();
mApp->setupCinderView( cinderView, cinder::app::RendererGl::create() );
mApp->launch();
}
- (void)updateUI
{
//Set new values...
}
@end
TestApp.h
#pragma once
#include "cinder/app/AppCocoaView.h"
class TestApp : public cinder::app::AppCocoaView {
public:
void setup();
void draw();
};
TestApp.cpp
#include "TestApp.h"
#include "cinder/gl/gl.h"
using namespace ci;
using namespace ci::app;
void TestApp::setup()
{
//Set values
//Call updateUI method in TestDelegate.mm
}
void TestApp::draw()
{
}
答案 0 :(得分:2)
以下内容应该有效:
TestDelegate.mm
#include "cinder/Cinder.h"
#import "TestDelegate.h"
@implementation TestDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// mApp = new TestApp;
// mApp->prepareLaunch();
// mApp->setupCinderView( cinderView, cinder::app::RendererGl::create() );
// add the following line
mApp->m_TestDelegate = self;
// mApp->launch();
}
@end
TestApp.h
#pragma once
#include "cinder/app/AppCocoaView.h"
@class TestDelegate;
class TestApp : public cinder::app::AppCocoaView {
public:
void setup();
void draw();
TestDelegate *m_TestDelegate;
};
TestApp.cpp
- &gt;已重命名为TestApp.mm
#include "TestApp.h"
#include "cinder/gl/gl.h"
#import "TestDelegate.h"
using namespace ci;
using namespace ci::app;
void TestApp::setup()
{
//Set values
//Call updateUI method in TestDelegate.mm
[this->m_TestDelegate updateUI];
}
注意:此代码是在浏览器中编写的,而我所做的Objective-C ++内容并没有使用ARC,所以如果它发出任何警告/错误,请告诉我,我会相应地更新代码。
答案 1 :(得分:1)
要调用实例方法,您需要一个实例。一旦你的C ++代码有一个指向该类实例的指针,你就可以将文件更改为Objective-C ++并发送一条正常的消息。