gpt4 book ai didi

c++ - 从 C++ 类调用 Objective C 实例方法?

转载 作者:行者123 更新时间:2023-11-30 02:48:14 24 4
gpt4 key购买 nike

如何从 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()
{

}

最佳答案

像下面这样的东西应该可以工作:

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 -> 重命名为 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,因此如果它给出任何警告/错误,请告诉我,我会相应地更新代码。

关于c++ - 从 C++ 类调用 Objective C 实例方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22154255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com