访问rct_export_method中的Objective-C类属性

时间:2018-09-11 20:04:46

标签: ios objective-c react-native native

我有一个Objective-c的UIViewController,它扩展了RCTBridgeModule并加载了一些JavaScript。但是,当我尝试在javascript中调用其方法之一时,无法访问所有UIViewController的属性。我相信这是因为仅从javascript调用其方法并不能完全初始化整个类,但是我找不到太多有关如何执行想要实现的文件的文档。任何帮助或指针,将不胜感激。这是我要执行的操作的简化示例:

edit:我当时想我可以使用[self.bridge.uiManager ...]然后使用rootTag访问该组件的实例,但是它无法识别桥实例上的uiManager。认为可能是因为它是视图控制器而不是UIView。

MyViewController.h:

 @interface MyViewController: UIViewController<RCTBridgeModule>
 @property PropertyClass *property
 @end

在另一个文件的类实例上设置属性。

MyViewController.m:

 - (void)viewDidLoad {
     [RCTRootView initialized here]

      ** _property is properly initialized **
 }

 RCT_EXPORT_MODULE();

 RCT_EXPORT_METHOD(methodName) {
     **trying to access _property here but it's nil**
 }

然后在我的JavaScript中:

MyFile.js

 const MyViewController = NativeModules.MyViewController;

 class MyFile extends React.Component {
     // some code here
     render() {
          MyViewController.methodName(); //calling into objective c here
     }
 }

0 个答案:

没有答案