反应本机本机模块RCTBubblingEventBlock为空

时间:2019-09-10 11:28:11

标签: ios swift react-native

尝试从本机模块创建回调,但未触发该回调。调用本机模块的代码似乎运行良好,并且可以打开扫描应用程序,但似乎根本无法使回调正常工作

这是我的代码。

@objc (ScanImageView)

class ScanImageViewController: UIViewController, ScanningViewControllerDelegate {

  @objc var onImageCapture: RCTBubblingEventBlock?

  @objc
  static func requiresMainQueueSetup() -> Bool {
    return true
  }

  @objc func startScan(){
    print("What had I created")
    DispatchQueue.main.async {
    self.getTopMostViewController()?.present(ScanningViewController(delegate: self), animated: true)
    }
  }

  func getTopMostViewController() -> UIViewController? {

    var topMostViewController = UIApplication.shared.keyWindow?.rootViewController

    while let presentedViewController = topMostViewController?.presentedViewController {
      topMostViewController = presentedViewController
    }
    return topMostViewController
  }

  func scanningViewControllerDidCancel(_ controller: ScanningViewController)   {
    print("Dismiss called")
    controller.dismiss(animated: true)
  }

  func scanningViewController(_ controller: ScanningViewController, didScan pointCloud: SCPointCloud) {
    print("scanningViewController called")
    print("scanningViewController called")
    if(self.onImageCapture != nil)
    {
      let event = ["startTime": "testing"]
      self.onImageCapture!(event)
      controller.dismiss(animated: true)
    }
  }
}

这是RCT_EXPORT_METHOD()宏的代码:

#import "React/RCTBridgeModule.h"
#import "React/RCTViewManager.h"

@interface RCT_EXTERN_MODULE(ScanImageView, UIViewController)
RCT_EXTERN_METHOD(startScan)
RCT_EXPORT_VIEW_PROPERTY(onImageCapture, RCTBubblingEventBlock)
@end

.js文件只有一个按钮,该按钮在单击时会调用startScan方法,并且似乎工作正常。

<button
    onPress={this.turnOn}
    title="Turn ON "
    color="#FF6347" 
    onImageCapture={this._onImageCapture}/>

根本没有被调用的onImageCapture。我已经尝试了过去5个小时。任何人都可以指导我正确的方向,以了解如何使回调工作于此。

谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了使用sendEvent(withName:“”,body:[])函数的解决方案。必须对我的代码进行以下更改

class ScanImageViewController: UIViewController, ScanningViewControllerDelegate

以上已更改为

class ScanImageViewController: RCTEventEmitter, ScanningViewControllerDelegate

还必须添加

@objc
  override func supportedEvents() -> [String]! {
    //Event names
    return ["onImageCapture", "onImageCompleted"]
  }

在.js文件上,您需要添加一个处理程序以侦听事件

ScanImageEvents.addListener('onImageCompleted', ({ imageData }) => {
      console.log('onImageCompleted')
    })

希望这对某人有帮助