Question

How can I call an Objective C instance method from a c++ class? In TestApp.cpp I would like to call updateUI in TestDelegate.mm

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()
{

}
Was it helpful?

Solution

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

#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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top