React native 在本机UIView中呈现JS组件时,OnPress不会';行不通

React native 在本机UIView中呈现JS组件时,OnPress不会';行不通,react-native,React Native,情况是: JS->Objective-C->JS 通常,我想在原生UIView中嵌入一个JS组件,JS视图应该响应用户交互事件,比如onPress事件 详情: 在我的JS组件中,我调用了IOS本机方法,如下所示: var CategoryManager = NativeModules.CategoryManager; CategoryManager.showCategoryPopup(); 在showCategoryPopup()objective-c方法中,我创建了一个uiview,并向父u

情况是:

JS->Objective-C->JS

通常,我想在原生UIView中嵌入一个JS组件,JS视图应该响应用户交互事件,比如onPress事件

详情: 在我的JS组件中,我调用了IOS本机方法,如下所示:

var CategoryManager = NativeModules.CategoryManager;
CategoryManager.showCategoryPopup();
在showCategoryPopup()objective-c方法中,我创建了一个uiview,并向父uiview添加了一个子视图,该子视图是使用RCTRootView类从JS组件创建的:

UIView *parentView = [UIView new];
RCTRootView *subview_1 = [[RCTRootView alloc] initWithBridge:[[RCCManager sharedIntance] getBridge] moduleName:@"CategoryViewPopup" initialProperties:nil];
[parentView addSubview: subview_1];
这里的
类别viewpopup
是一个JS组件:

export default class CategoryViewPopup extends Component {
  constructor(props) {
    super(props);
    this.state = {
      textToggle: false,
    };
  }

  render() {
    return (
      <TouchableOpacity onPress={this.toggleText.bind(this)}>
        <View style={{width: 100, height: 100, backgroundColor: 'red'}}>
          <Text>{this.state.textToggle ? "Text 1" : "Text 2"}</Text>
        </View>
      </TouchableOpacity>
    );
  }

  toggleText() {
    this.setState({
      textToggle: !this.state.textToggle,
    });
  }
}
导出默认类CategoryViewPopup扩展组件{
建造师(道具){
超级(道具);
此.state={
textToggle:false,
};
}
render(){
返回(
{this.state.textToggle?“文本1”:“文本2”}
);
}
toggleText(){
这是我的国家({
textToggle:!this.state.textToggle,
});
}
}
但是,
onPress
事件不起作用

谢谢

导出默认类CategoryViewPopup扩展组件{
export default class CategoryViewPopup extends Component {
  constructor(props) {
    super(props);
    this.state = {
      textToggle: false,
    };
  }

  render() {
    return (
      <View style = {styles.container}>
      <TouchableOpacity onPress={this.toggleText.bind(this)}>
        <View style={{width: 100, height: 100, backgroundColor: 'red'}}>
          <Text>{this.state.textToggle ? "Text 1" : "Text 2"}</Text>
        </View>
      </TouchableOpacity>
      </View>
    );
  }

  toggleText() {
    this.setState({
      textToggle: !this.state.textToggle,
    });
  }
}

const styles = StyleSheet.create({
  container : {
    flex : 1,
    backgroundColor : 'rgba(244,244,244,0.9)',
    justifyContent : 'center',
    alignItems : 'center'
  }
});
建造师(道具){ 超级(道具); 此.state={ textToggle:false, }; } render(){ 返回( {this.state.textToggle?“文本1”:“文本2”} ); } toggleText(){ 这是我的国家({ textToggle:!this.state.textToggle, }); } } const styles=StyleSheet.create({ 容器:{ 弹性:1, 背景颜色:“rgba(244244244,0.9)”, 为内容辩护:“中心”, 对齐项目:“中心” } });
这不是风格问题,我已经测试过你的代码,它也不起作用。谢谢即使我已经测试过,并且在印刷时,它正在将文本Text2更改为Text1,反之亦然……您是使用将CategoryViewPopup组件嵌入UIView进行测试,还是直接在JS组件中呈现CategoryViewPopup?如果您直接在JS组件中呈现CategoryViewPopup,我知道它显然会工作,但我想要的是将CategoryViewPopup嵌入UIView中,CategoryViewPopup也应该接受用户交互事件(onPress),但结果不起作用。@rockllei我想我可以帮助你,你可以将我添加到Skype上,即rajeev.upadhyay72,这样我就可以给你解决方案了!!!因为我是专业的iOS开发者,也在使用reactNative。