ios - 将 React Native RCTRootView 关闭回 Native ViewController

标签 ios swift react-native react-navigation react-navigation-stack

我正在创建一个应用来将 react-native 与现有 Swift 应用集成。

我研究过类似的问题:

在遵循不同的教程时:

还有官方docs

问题是:所有这些都已经过时了(除了文档)。他们中的大多数使用旧版Navigation,而不是Stack Navigator。其中一个教程(带有星号的教程)展示了如何使用应用程序的 rootTag 将 React Native 应用程序关闭回 native 应用程序,但同样,这是使用旧版 完成的导航

如果我尝试执行相同的操作,我将无法从我的应用中看到 props

我有一个 Storyboard,其中有一个 Button,单击该按钮时会调用此 UIViewController:

按钮 Controller

import Foundation
import UIKit
import React

class ButtonController: UIViewController  {
    @IBOutlet weak var button: UIButton!

    @IBAction func buttonClicked(_ sender: Any) {
        let data:[String : String] = ["onNavigationStateChange": "{handleNavigationChange}",
                                      "uriPrefix":"/app"];
        let rootView = MixerReactModule.sharedInstance.viewForModule("ReactNativeApp", initialProperties: data)

        let viewController = UIViewController()
        viewController.view = rootView
        self.present(viewController, animated: true, completion: nil)
    }
}

当我启动应用程序时,我可以看到:

2019-10-23 10:29:30.021 [info][tid:com.facebook.react.JavaScript] Running "ReactNativeApp" with {"rootTag":1,"initialProps":{"uriPrefix":"/app","onNavigationStateChange":"{handleNavigationChange}"}}

但是当我尝试访问 React Native 代码上的 this.props 属性时,我得到 undefined

index.js

import HomeScreen from './components/HomeScreen'
import {AppRegistry} from 'react-native';
import {createAppContainer} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';

const MainNavigator = createStackNavigator({
  Home: {screen: HomeScreen}
}, {
  initialRouteName: 'Home'
})

const NavigationApp = createAppContainer(MainNavigator);

AppRegistry.registerComponent('ReactNativeApp', () => NavigationApp)

console.log("NANANA1", this)
console.log("NANANA2", this.routeName)
console.log("NANANA3", MainNavigator)
console.log("NANANA4", MainNavigator.props)
console.log("NANANA5", NavigationApp)
console.log("NANANA6", NavigationApp.props)

export default NavigationApp

HomeScreen.js

import React from 'react';
import {Text, View, Button, NativeModules} from 'react-native';

var RNBridge = NativeModules.RNBridge;

export default class HomeScreen extends React.Component {
    static navigationOptions = {
      headerTitle: () => (
        <Text>'Welcome'</Text>
      ),
      headerLeft: () => (
        <Button title="Dismiss" onPress={() => {
          console.log("WOLOLO: ", RNBridge)
          console.log("ROGAN: ", this._reactInternalFiber.tag)
          RNBridge.dismissPresentedViewController(this._reactInternalFiber.tag)
          // RNBridge.dismissPresentedViewController(1)
        }}/>
      )
    };
    render() {
      console.log('KIKIKI', this._reactInternalFiber.tag)
      console.log("MMMMMMM: ", this.props)
      return (
        <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
          <Text>Home Screen</Text>
        </View>
      );
    }
}

这些是我在 RN 中用来生成 View 的 2 个文件。我已经尝试了很多方法来获取 rootTag 值,唯一提供此值的方法是 (tagrootTag on XCode相同 (1))

this._reactInternalFiber.tag

但我不知道如何将这些值发送到我的 headerLeft 方法以使用相同的标记,以便当我按下 Dismiss 按钮时它会调用 Swift 代码对于dismissPresentedViewController

如何有效地解雇我的 VC?或者至少将 rootTag 从 Swift 传递到我的 headerLeft() 方法?

我正在使用这些版本的 react-native:

react-native-cli: 2.0.1
react-native: 0.61.2

最佳答案

如果您希望关闭 RN View 中呈现的 native swift View Controller

self.dismiss(animated: true, completion: nil)

我展示了如下的快速 View Controller

 let modelVC = ModelViewController()  <-- sub class of UIViewController

 DispatchQueue.main.async {
   let navController = UINavigationController(rootViewController: modelVC)
   navController.modalPresentationStyle = .fullScreen
   let topController = UIApplication.topMostViewController()
   topController?.present(navController, animated: true, completion: nil)
 }

我在 UIApplication 上有一个扩展,可以找出上面使用的最顶层 View Controller 是什么

extension UIApplication {

  class func topMostViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {

    if let navigationController = controller as? UINavigationController {
      return topMostViewController(controller: navigationController.visibleViewController)
    }

    if let tabController = controller as? UITabBarController {
      if let selected = tabController.selectedViewController {
        return topMostViewController(controller: selected)
      }
    }

    if let presented = controller?.presentedViewController {
      return topMostViewController(controller: presented)
    }

    return controller
  }
}

关于ios - 将 React Native RCTRootView 关闭回 Native ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58527243/

相关文章:

arrays - 尝试将本地函数中的 Firebase 项目附加到全局数组(数组必须是本地数组)

swift - 如何在 Swift 4 中使用 #selector() 处理 @objc 推理弃用?

react-native - 如何将 jest 特定设置添加到 create-react-native-app?

ios - 嵌入式 UIVIewController 在触摸或滚动时意外水平扩展

ios - 区分 UserLocation Pin 和用户添加的 Pin 之间的区别

ios - 如何使用 Swift 泛型传递类型并返回该类型的对象?

ios - 升级到 Xcode 7 后无法写入文档目录

react-native - React Native 和使用 websockets

javascript - 从渲染方法中的嵌套函数 react 调用组件的方法

iphone - 我在 Xcode 4.5.2 中收到 'deprecated' 警告