【问题标题】:How to call objective c method from c method iOS如何从 c 方法 iOS 调用目标 c 方法
【发布时间】:2017-03-16 07:16:52
【问题描述】:

我想从 c => 目标 c (ios) 调用数据。

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

【问题讨论】:

  • 为什么要在vc类中创建c函数?您的所有代码似乎都是客观的 c
  • 我从一个 c 文件中调用了 CameraCaptureFromC。我收到了 thisClass 错误。因为它是 self,但这会出错。
  • -(void)cameraCapture{} 是 Objective-C 方法,void CameraCaptureFromC()... 是 C 函数。但是当您执行cameraCapture() 时,您是在说它是一个 C 函数,而它不是。另外,请不要张贴截图。第一个原因?我在答案/评论中添加手动复制/编写您的代码以指出问题。显然,开发人员很懒惰。此外,图像往往会随主机消失。

标签: ios objective-c c void


【解决方案1】:

声明id引用变量

#import <Cocoa/Cocoa.h>

id refToSelf;

比使用调用

[refToSelf cameraCapture];

【讨论】:

  • 是的。这会有所帮助,但我得到“访问 _cachedSystemAnimationFence 需要主线程”
  • @NovusMobile 只需在主线程中调用方法。因为如果您在 ui 中进行更改,则与主线程中调用的所有函数相比
【解决方案2】:

到目前为止,您已经得到了正确的线条。但是您忘记在 .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 ...
  }

希望对您有所帮助。

【讨论】:

    【解决方案3】:

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

    void CameraCaptureFromC(long cCameraClient,
                         const char *imageName,
                                  void *object,
                      MasterViewController *vc) {
      NSLog(@"glfm:Camera Call");
      [vc addAllImages];
      //your code
    }
    

    【讨论】:

      【解决方案4】:
      //
      //  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
      

      控制台日志

      test123 2017-03-16 13:22:02.858 test[1198:240019] 消息:从​​ C 方法调用目标 C 方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-22
        • 1970-01-01
        相关资源
        最近更新 更多