原生ios调用不同的RN界面



  • 如下代码,我想在点击A时,进入由moduleA的viewcontroller,点击B时,进入由moduleB的viewcontroller,但RN只能注册一个moduleA,进入B界面就会出错,请问原生ios调用不同的RN界面是怎么解决的?先谢谢各位大神。

    // 整体js模块的名称
    AppRegistry.registerComponent('ModuleA', () => ModuleA);
    
    // 进入界面A
    @IBAction func tapA(_ sender: UIButton) {
            let jsCodeLocation = Bundle.main.url(forResource: "main", withExtension: "jsbundle")
            let mockData:NSDictionary = ["scores": "100"]
            let rootView = RCTRootView(
                bundleURL: jsCodeLocation,
                moduleName: "ModuleA",
                initialProperties: mockData as [NSObject : AnyObject],
                launchOptions: nil
            )
            let vc = UIViewController()
            vc.view = rootView
            navigationController?.pushViewController(vc, animated: true)
        }
    
    // 进入界面B
    @IBAction func tapB(_ sender: UIButton) {
            let jsCodeLocation = Bundle.main.url(forResource: "main", withExtension: "jsbundle")
            let mockData:NSDictionary = ["age": "10"]
            let rootView = RCTRootView(
                bundleURL: jsCodeLocation,
                moduleName: "ModuleB",
                initialProperties: mockData as [NSObject : AnyObject],
                launchOptions: nil
            )
            let vc = UIViewController()
            vc.view = rootView
            navigationController?.pushViewController(vc, animated: true)
        }
    


  • 1、RN可以根据moduleName注册多个;
    2、可以在initialPropertyies中附加参数,用于匹配不同的界面,这个是针对单一moduleName的情况

    当然,你也可以两个结合起来用的


登录后回复