gpt4 book ai didi

ios - React Native Native模块RCTBubblingEventBlock为null

转载 作者:行者123 更新时间:2023-11-29 05:28:53 25 4
gpt4 key购买 nike

尝试从 native 模块创建回调,但回调未被触发。调用 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个小时我一直在努力。任何人都可以指导我正确的方向,了解如何让回调为此工作。

提前致谢。

最佳答案

我找到了使用 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')
})

希望这对某人有帮助

关于ios - React Native Native模块RCTBubblingEventBlock为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57869850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com