如何从 c 方法 iOS 调用 objective c 方法

How to call objective c method from c method iOS

我想从 c 调用数据 => objective c (ios).

下面我分享了我的代码截图。

声明id引用变量

#import <Cocoa/Cocoa.h>

id refToSelf;

然后使用

调用
[refToSelf cameraCapture];

到目前为止你已经得到了正确的线条。但是你忘了在你的 .c

中包含 .h 文件

假设您 myiosclass.h 想要调用 mycclass.c 文件中的 cameraCapture() 方法。

在你的mycclass.c

 #include "myiosclass.h"

    void cameraCapture(){
    //your c code goes here...
    }

在你的myiosclass.h

extern void cameraCapture();

在你的myiosclass.m

-(void)myMethod {
    cameraCapture(); //Called your C method.

    //Rest of objective c code ...
  }

希望对您有所帮助。

在你的 c 方法中将当前视图控制器作为参数传递并使用它

void CameraCaptureFromC(long cCameraClient,
                     const char *imageName,
                              void *object,
                  MasterViewController *vc) {
  NSLog(@"glfm:Camera Call");
  [vc addAllImages];
  //your code
}
//
//  ViewController.m
//  test
//
//  Created by colorsMacBook on 16/03/17.
//  Copyright © 2017 colors software. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end



@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    someMethod();
}

void someMethod(){
    printf("test123\n");
    [ViewController someMethod2:@"Calling objective C Method from C  method"];
}
+(void)someMethod2:(id)sender{
    NSLog(@"Message: %@",sender);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

控制台日志

测试123 2017-03-16 13:22:02.858 test[1198:240019] 消息:从​​ C 方法

调用 objective C 方法