我正在创建一个将react-native
与现有Swift应用集成的应用。
我研究过类似的问题:
同时遵循不同的教程:
和官方docs
问题是:所有这些都已过时(但是文档)。他们中的大多数使用旧版Navigation
,而不是Stack Navigator
。其中一篇教程(带星号的教程)展示了如何使用应用程序的rootTag
将React Native应用程序撤回本机应用程序,但是同样,这是通过旧版Navigation
完成的。
[如果尝试执行相同的操作,则无法从我的应用程序中看到props
。
我有一个情节提要,里面有一个Button
,单击后将其称为UIViewController
:
ButtonController
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
值,并且似乎唯一提供此值的是(XCode上的tag
和rootTag
相同(1
))
this._reactInternalFiber.tag
但是我不知道如何将这些值发送到我的headerLeft
方法以使用相同的标签,因此当我按下Dismiss
按钮时,它将调用dismissPresentedViewController
的Swift代码。
如何有效地撤消我的风险投资?还是至少让rootTag
从Swift传递到我的headerLeft()
方法?
我正在使用这些版本的react-native
:
react-native-cli: 2.0.1
react-native: 0.61.2
如果您希望解散从RN View中显示的本机swift view控制器,则>]
self.dismiss(animated: true, completion: nil)
我如下所示提供了快速视图控制器
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
}
}