在iOS平台中怎样使用c++和objective
1个回答
展开全部
目的:
当Cocos2dx场景中一个按钮被点击,我必须向Objective-C发送消息来显示本地(本来的系统中)的弹框。让我们找到如何实现它。
包含
你需要在你的项目中引用若干个文件,我已经创建了一个网络存储库,并根据环境不同将其分开。请确保你引用了所有的C++和Objective-C文件。以下是网络存储库的链接:
EasyNDK
C++
#include "NDKHelper.h"
// The button click method of Cocos2dx
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// Register a selector in a global space
// So that when our native environment will call the method with the string
// It can respond to the selector
// Note : Group name is there for ease of removing the selectors
NDKHelper::AddSelector("HelloWorldSelectors",
"SampleSelector",
callfuncND_selector(HelloWorld::SampleSelector),
this);
// Making parameters for message to be passed to native language
// For the ease of use, i am sending the method to be called name from C++
CCDictionary* prms = CCDictionary::create();
prms->setObject(CCString::create("SampleSelector"), "to_be_called");
// Finally call the native method in current environment
SendMessageWithParams(string("SampleSelector"), prms);
}
// A selector that will respond to us, when native language will call it
void HelloWorld::SampleSelector(CCNode *sender, void *data)
{
CCLog("Called from native environment");
}
// Destructor to remove all the selectors which are grouped as HelloWorldSelectors 31HelloWorld::~HelloWorld()
{
// Remove the associated selector group from the global space,
// Because we are destroying this instance
NDKHelper::RemoveSelectorsInGroup("HelloWorldSelectors");
}
Objective-C
// Using a UIViewController init method to attach a receiver for messages from C++
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
// Tell NDKHelper that RootViewController will respond to messages
// Coming from C++
[IOSNDKHelper SetNDKReciever:self];
}
return self;
}
// Implement the selector to be called for a message from C++ 14// Be sure to name the selector to be of the same string as you will pass from C++
// Like we passed "SampleSelector" from C++, that is why created this selector
- (void) SampleSelector:(NSObject *)prms
{
NSLog(@"purchase something called");
NSDictionary *parameters = (NSDictionary*)prms;
NSLog(@"Passed params are : %@", parameters);
// Fetching the name of the method to be called from Native to C++ 23 // For a ease of use, i have passed the name of method from C++
NSString* CPPFunctionToBeCalled = (NSString*)[parameters objectForKey:@"to_be_called"];
// Show a bogus pop up here
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
message:@"This is a sample popup on iOS"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
// Send C++ a message with paramerts
// C++ will recieve this message, only if the selector list will have a method
// with the string we are passing
[IOSNDKHelper SendMessage:CPPFunctionToBeCalled WithParameters:nil];
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询