gpt4 book ai didi

react-native - 如何从 React Native 中解除 View Controller ?

转载 作者:行者123 更新时间:2023-12-04 03:56:58 25 4
gpt4 key购买 nike

Integration with exsiting apps doc展示了如何呈现内容为 react native 的 View Controller 。

RCTRootView *rootView =
[[RCTRootView alloc] initWithBundleURL: jsCodeLocation
moduleName: @"RNHighScores"
initialProperties:
@{
@"scores" : @[
@{
@"name" : @"Alex",
@"value": @"42"
},
@{
@"name" : @"Joel",
@"value": @"10"
}
]
}
launchOptions: nil];
UIViewController *vc = [[UIViewController alloc] init];
vc.view = rootView;
[self presentViewController:vc animated:YES completion:nil];

它没有告诉你的是如何关闭 viewcontroller,因为我们在 react native(javascript 世界)而不是 objc/swift 世界了..

如何在 View Controller 上调用原生 dimiss... 函数?

最佳答案

查看此存储库:tejasd/ios-playground

基本思路:

1.给React Native页面导入一个iOS原生类,这个iOS类做一件事:发布通知

let DismissViewControllerManager = NativeModules.DismissViewControllerManager;

2.在React Native页面中发布通知

onButtonPress: () => {
DismissViewControllerManager.goBack();
}

3.在 native iOS 页面中处理该通知并调用 dismiss

// add observer in viewDidLoad
NotificationCenter.default.addObserver(self,
selector: #selector(goBack(_:)),
name: NSNotification.Name("dismissViewController"),
object: nil)

// remove observer in deinit (iOS 8 or earlier)
NotificationCenter.default.removeObserver(self)

// handle dismiss notification
func goBack(_ sender: Any?) -> Void {
self.dismiss(animated: true, completion: nil)
}

关于react-native - 如何从 React Native 中解除 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45273921/

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