2016-01-12 22 views
0

我正在编写一个库,我想将自定义属性添加到现有的React Native视图中,这些视图将由我的库处理。因此,例如React原生视图的自定义属性

<View my_custom_property=“something”> 
    <TextView my_custom_property=“something_else”>Hello</TextView> 
</View> 

而且我想我objc库访问UIViewRCTView这个属性被应用到。有人有主意吗?

回答

1

好的,这是我可以提供的最佳解决方案。我使用findNodeHandle将元素的标识符传递给我的RCTBridgeModule,然后使用RCTUIManager获取UIView

在我的index.ios.js(或任何你想访问元素的地方),我将创建一个对我的元素的引用,并通过调用findNodeHandle来传递该元素的ID给我的桥模块。

var React = require('react-native'); 
var { findNodeHandle } = React; 
const MyCustomBridge = React.NativeModules.MyCustomBridge; 

var TestFairy = React.createClass({ 
    componentDidMount: function() { 
    MyCustomBridge.something(findNodeHandle(this.refs.instructions)); 
    }, 

    render: function() { 
    return (
     <Text style={styles.instructions} ref="instructions">Hello</Text> 
    ); 
    } 
}); 

然后在RCTMyCustomBridge.m,我会转换,从findNoderHandle接收到使用RCTUIManager#addUIBlock一个UIView ID。重要的是你在正确的线程上调用该方法,所以我使用RCTUIManager#methodQueue并调用dispatch_async

RCT_EXPORT_METHOD(something:(nonnull NSNumber *)reactTag) { 
    dispatch_async(_bridge.uiManager.methodQueue, ^{ 
     [_bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) { 
      UIView *view = viewRegistry[reactTag]; 
      if (view != nil) { 
       // Do the magic! 
      } 
     }]; 
    }); 
} 

但愿这是有人试图访问的基本UIView一个阵营本土元素是有用的。这不完全是我最初的问题所要求的,然而,它是完成同样事情的一种迂回方式。

+0

我做同样的事情。但有时UIBlock将不会执行,直到调用此函数后的下一次用户交互。你以前见过吗? –