【问题标题】:Get the presented Viewcontroller from ReactNative rootviewcontroller从 ReactNative rootviewcontroller 获取呈现的 Viewcontroller
【发布时间】:2018-05-25 17:18:48
【问题描述】:

在 React Native 项目中,我想从 iOS npm 模块访问 Presented Viewcontroller。我可以使用下面的代码访问 RN 项目的 rootviewcontroller

UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;

但我想要在 RootVC 之上呈现的当前 VC,以便我应该能够在其之上呈现原生 (iOS) UINavigationController。

注意:[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController 返回零。

【问题讨论】:

    标签: ios react-native npm react-native-native-module


    【解决方案1】:

    我知道很久以前有人问过这个问题,但是我今天遇到了同样的问题([UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController 返回nil)并且暂时找不到解决方案,所以我将把它留在这里有人还在寻找。

    React Native 源代码中有一个function,可让您确定呈现的视图控制器。似乎他们将其用于他们的 ActionSheetIOS 模块(请参阅此line)。要在您自己的本机模块中使用它,请添加以下内容:

    // Put this near the top of the file
    #import <React/RCTUtils.h>
    
    ...
    
    // Put this where you need access to the presented view controller
    UIViewController *presentedViewController = RCTPresentedViewController();
    

    【讨论】:

    • 请注意,如果您想在 Swift 中访问 RCTPresentedViewController,可以将 #import &lt;React/RCTUtils.h&gt; 行添加到您的 &lt;ModuleName&gt;-Bridging-Header.h 文件中。
    猜你喜欢
    • 2018-03-07
    • 2021-12-15
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    相关资源
    最近更新 更多