简体   繁体   English

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

[英]Call an Objective C instance method from a c++ class?

How can I call an Objective C instance method from a c++ class? 如何从c ++类调用Objective C实例方法? In TestApp.cpp I would like to call updateUI in TestDelegate.mm 在TestApp.cpp中,我想在TestDelegate.mm中调用updateUI

TestDelegate.h 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 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 TestApp.h

#pragma once
#include "cinder/app/AppCocoaView.h"

class TestApp : public cinder::app::AppCocoaView {
  public:
    void                setup();
    void                draw();
};

TestApp.cpp 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()
{

}

Something like the following ought to work: 像下面这样的东西应该工作:

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 -> renamed to TestApp.mm 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];

}

Note: this code was written in a browser, and the Objective-C++ stuff I've done doesn't use ARC, so if it gives any warnings/errors, let me know and I'll update the code accordingly. 注意:这段代码是用浏览器编写的,我所做的Objective-C ++的东西不使用ARC,所以如果它给出任何警告/错误,请告诉我,我会相应地更新代码。

To call an instance method, you need an instance. 要调用实例方法,您需要一个实例。 Once your C++ code has a pointer to an instance of the class, you can just change the file to Objective-C++ and send a message like normal. 一旦你的C ++代码有一个指向该类实例的指针,你就可以将文件更改为Objective-C ++并像平常一样发送消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM