【问题标题】:Call an Objective C instance method from a c++ class?从 c++ 类调用一个 Objective C 实例方法?
【发布时间】:2014-03-03 18:19:00
【问题描述】:

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

}

【问题讨论】:

标签: c++ objective-c objective-c++


【解决方案1】:

类似下面的东西应该可以工作:

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

【讨论】:

  • 非常感谢,工作得很好,正如我所料,我尝试了所有错误的方法来解决它。一件事:Xcode 在 TestApp.mm Instance method '-updateUI' not found (return type defaults to 'id') 中给出警告
  • @davivid:您确定在您的 TestApp.mm 文件中包含了 #import "TestDelegate.h" 行吗?
【解决方案2】:

要调用实例方法,您需要一个实例。一旦您的 C++ 代码有一个指向该类实例的指针,您就可以将文件更改为 Objective-C++ 并像往常一样发送消息。

【讨论】:

  • 您能否详细说明如何在所示框架内创建实例和指针?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 2012-02-09
  • 2023-03-29
  • 2012-12-12
  • 1970-01-01
  • 2015-07-10
相关资源
最近更新 更多