1

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome thanks, working great, as I expected I was trying all the wrong ways to go about it. One thing: Xcode gives a warning in in TestApp.mm Instance method '-updateUI' not found (return type defaults to 'id')
@davivid: Are you sure you included the #import "TestDelegate.h" line in your TestApp.mm file?
1

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.

1 Comment

Could you give more details as to how I can create the instance and pointer within the framework shown?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.