我有个问题。 我需要从我的iPad应用程序中通过C ++函数为UITextView控制器赋值。所以,C ++函数返回一个字符串,我可以在输出窗口中看到该输出。我使用.mm文件合并了C ++和XCode目标C.现在,我需要从C ++函数中获取值并将它们添加到UITextFiled中。
比如说。 我的C ++函数如下: .cpp文件
void *consumer (void* data)
{
SyncBuffThang<GLOBAL_BUFF_LEN,GLOBAL_BUFFS>* cc =(SyncBuffThang<GLOBAL_BUFF_LEN,GLOBAL_BUFFS>*) data;
affinity("consumer", cons);
for (int ii=0; ii<100; ii++)
{
unsigned char c = cc->get();
cc->res = c;
myVar = c;
cerr << "Consumer Get" << myVar << endl;
f +=c;
}
cerr << "Leaving consumer in method cons" << f << endl;
return 0;
}
int PC9::RunPC()
{
SyncBuffThang<GLOBAL_BUFF_LEN, GLOBAL_BUFFS> pc;
pthread_t p, c;
pthread_create(&p, 0, producer, &pc);
pthread_create(&c, 0, consumer, &pc);
pthread_join(p, 0);
pthread_join(c,0);
}
以下播放.mm文件代码。
#import "PC.h"
#import "PC9.h"
#import "GV.h"
@implementation PC
-(void)callFunctionPC
{
PC9 * myCPlusPlusObj; //A C++ object
myCPlusPlusObj=new PC9();
myCPlusPlusObj-> RunPC();
}
@end
在上面的函数中看到我可以打印myVar但是,我不知道如何访问它或从我的iPad应用程序在UITextFile上查看它。
朋友们,我非常感谢你们的帮助。
提前致谢。
-T
答案 0 :(得分:1)
这是我一直在等待的答案!!!
HelloWorld.h
#ifndef __Demo1__HelloWorld__
#define __Demo1__HelloWorld__
#include <iostream>
#endif
class HelloWorld {
public:
std::string Mtd_HelloWorld();
};
HelloWorld.cpp
#include "HelloWorld.h"
std::string HelloWorld::Mtd_HelloWorld()
{
std::string output;
output = "This is from C++";
return output;
}
HelloWorldM.h
#import <Foundation/Foundation.h>
@interface HelloWorld_M : NSObject
-(NSString *)CallCPP;
-(UIView *)CreateTextView:(NSString *)input;
@end
HelloWorld.mm
#import "PCViewController.h"
#include "HelloWorld.h"
#include "HelloWorld_M.h"
@implementation HelloWorld_M
-(NSString *)CallCPP
{
HelloWorld * myCPlusPlusObj; //A C++ object
myCPlusPlusObj=new HelloWorld();
std::string res = myCPlusPlusObj-> Mtd_HelloWorld();
NSString *result = [NSString stringWithCString:res.c_str()
encoding:[NSString defaultCStringEncoding]];
return result;
}
-(UIView *)CreateTextView:(NSString *)input
{
UITextView *myTextView = [[UITextView alloc] initWithFrame: CGRectMake (0.0,0.0,320.0,200.0)];
myTextView.text = input;
return myTextView;
}
@end
PCViewController.h
#import <UIKit/UIKit.h>
#import "HelloWorld_M.h"
@interface PCViewController : UIViewController
{
HelloWorld_M *objHelloWorld;
}
- (IBAction)RunHelloWorld:(id)sender;
@end
PCViewController.m
#import "PCViewController.h"
@interface PCViewController ()
@end
@implementation PCViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)RunHelloWorld:(id)sender
{
objHelloWorld = [[HelloWorld_M alloc]init];
[self.view addSubview: [objHelloWorld CreateTextView:[objHelloWorld CallCPP]]] ;
}
@end