gpt4 book ai didi

ios - 将 React Native RCTRootView 关闭到 Native ViewController

转载 作者:行者123 更新时间:2023-11-28 11:24:54 32 4
gpt4 key购买 nike

我正在创建一个应用程序以将 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 值,而唯一似乎提供此值的方法是 XCode 上的 (tagrootTag相同 (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)

我将 swift 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com