Ios ReactNative-如何在ViewController中调用函数

Ios ReactNative-如何在ViewController中调用函数,ios,swift,react-native,Ios,Swift,React Native,我是ReactNative新手,我正在尝试将ReactNative集成到我当前的项目中 在my ViewController中包含RCTrotView和实现如下的代码: ViewController.swift override func viewDidLoad() { super.viewDidLoad() ..... let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider

我是ReactNative新手,我正在尝试将ReactNative集成到我当前的项目中

在my ViewController中包含RCTrotView和实现如下的代码:

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()        
    .....
        let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider: nil, launchOptions: nil)

        let reactRootView = RCTRootView(bridge: bridge, moduleName: "Index", initialProperties: props)

        self.view.addSubview(reactRootView);
        reactRootView.frame = self.view.bounds
        reactRootView.delegate = self;
    ....     
}

func onReactEvent(event :NSDictionary , callback: RCTResponseSenderBlock) {}
@objc func sendCustomEventToHost(event: NSDictionary, callback: RCTResponseSenderBlock) -> Void {}
let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider: nil, launchOptions: ["controller":self"])
let controller = self.bridge.launchOptions["controller"]
反应管理器。swift

override func viewDidLoad() {
    super.viewDidLoad()        
    .....
        let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider: nil, launchOptions: nil)

        let reactRootView = RCTRootView(bridge: bridge, moduleName: "Index", initialProperties: props)

        self.view.addSubview(reactRootView);
        reactRootView.frame = self.view.bounds
        reactRootView.delegate = self;
    ....     
}

func onReactEvent(event :NSDictionary , callback: RCTResponseSenderBlock) {}
@objc func sendCustomEventToHost(event: NSDictionary, callback: RCTResponseSenderBlock) -> Void {}
let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider: nil, launchOptions: ["controller":self"])
let controller = self.bridge.launchOptions["controller"]
因此,我如何在我的视图控制器中调用onReactEvent()函数,从ReactNativeManager中的函数sendCustomEventToHost()
谢谢

ReactNativeManager
中,您必须创建ViewController的实例。 例如:

let viewC = ViewController()
然后你只要打电话:

viewC.onReactEvent(....)

我知道如何使用启动选项将ViewController传递给ReactNativeManager

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()        
    .....
        let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider: nil, launchOptions: nil)

        let reactRootView = RCTRootView(bridge: bridge, moduleName: "Index", initialProperties: props)

        self.view.addSubview(reactRootView);
        reactRootView.frame = self.view.bounds
        reactRootView.delegate = self;
    ....     
}

func onReactEvent(event :NSDictionary , callback: RCTResponseSenderBlock) {}
@objc func sendCustomEventToHost(event: NSDictionary, callback: RCTResponseSenderBlock) -> Void {}
let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider: nil, launchOptions: ["controller":self"])
let controller = self.bridge.launchOptions["controller"]
访问 反应管理器。swift

override func viewDidLoad() {
    super.viewDidLoad()        
    .....
        let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider: nil, launchOptions: nil)

        let reactRootView = RCTRootView(bridge: bridge, moduleName: "Index", initialProperties: props)

        self.view.addSubview(reactRootView);
        reactRootView.frame = self.view.bounds
        reactRootView.delegate = self;
    ....     
}

func onReactEvent(event :NSDictionary , callback: RCTResponseSenderBlock) {}
@objc func sendCustomEventToHost(event: NSDictionary, callback: RCTResponseSenderBlock) -> Void {}
let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider: nil, launchOptions: ["controller":self"])
let controller = self.bridge.launchOptions["controller"]

希望这有帮助

因为我想在之前初始化的ViewController中使用一些变量。代码“let viewC=ViewController()”将初始化具有不同地址的新控制器。我不知道如何调用父ViewController的方法,该父ViewController包含从ViewController推送的RCTrootViewArractNativeManager?ReactNativeManager类继承自RCTBridgeModule,由React Native view启动,其方法由React Native view调用。