访问数据从c++函数到UITextView控制器

Accessing data from C++ function into UITextView controller

本文关键字:UITextView 控制器 函数 c++ 数据 访问      更新时间:2023-10-16

我有一个问题。我需要分配值到UITextView控制器在我的iPad应用程序从一个c++函数。c++函数返回一个字符串,我可以在输出窗口看到输出。我用。mm文件合并了c++和XCode objective C。现在,我需要从c++函数中获取值,并将它们添加到uitextfield中。

比如说。我的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

这就是我一直在等待的答案!!

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