关闭 React Native RCTRootView 返回 Native ViewController
Dismiss a React Native RCTRootView back to a Native ViewController
我正在创建一个应用程序以将 react-native
与现有的 Swift 应用程序集成。
我调查过类似的问题:
- React-Native: Dismiss/Exit React-Native View back to Native
- How can I go back to native view controller from react-native page?
在学习不同的教程时:
- React Native calling class methods on native Swift
- Swift in React Native the ultimate guide
- React Native tutorial integrating in an existing app*
和官方docs
问题是:所有这些都已过时(但文档除外)。他们中的大多数使用遗留 Navigation
而不是 Stack Navigator
。其中一个教程(带星号的那个)展示了如何使用应用程序的 rootTag
将 React Native 应用程序关闭回 Native 应用程序,但同样,这是通过遗留 Navigation
完成的。
如果我尝试这样做,我将无法从我的应用程序中看到 props
。
我有一个故事板,里面有一个 Button
,当点击它时会调用它 UIViewController
:
按钮控制器
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 中用来生成视图的 2 个文件。我已经尝试了很多方法来获得 rootTag
值,唯一似乎提供此值的是(tag
和 XCode 上的 rootTag
是相同的(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
呈现的本机 swift 视图控制器
self.dismiss(animated: true, completion: nil)
我展示 swift 视图控制器如下
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 上有一个扩展来找出上面使用的最顶层视图控制器是什么
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
}
}
我正在创建一个应用程序以将 react-native
与现有的 Swift 应用程序集成。
我调查过类似的问题:
- React-Native: Dismiss/Exit React-Native View back to Native
- How can I go back to native view controller from react-native page?
在学习不同的教程时:
- React Native calling class methods on native Swift
- Swift in React Native the ultimate guide
- React Native tutorial integrating in an existing app*
和官方docs
问题是:所有这些都已过时(但文档除外)。他们中的大多数使用遗留 Navigation
而不是 Stack Navigator
。其中一个教程(带星号的那个)展示了如何使用应用程序的 rootTag
将 React Native 应用程序关闭回 Native 应用程序,但同样,这是通过遗留 Navigation
完成的。
如果我尝试这样做,我将无法从我的应用程序中看到 props
。
我有一个故事板,里面有一个 Button
,当点击它时会调用它 UIViewController
:
按钮控制器
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 中用来生成视图的 2 个文件。我已经尝试了很多方法来获得 rootTag
值,唯一似乎提供此值的是(tag
和 XCode 上的 rootTag
是相同的(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
呈现的本机 swift 视图控制器self.dismiss(animated: true, completion: nil)
我展示 swift 视图控制器如下
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 上有一个扩展来找出上面使用的最顶层视图控制器是什么
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
}
}