2

我有以下 TabBarIOS.Item 设置:

          <TabBarIOS.Item
            selected={this.state.selectedTab === 'tab1'}
            title='Tab 1'
            icon={require('./Components/Icons/IconImages/Tab1Icon.png')}
            onPress={() => {
              this.setState({
                selectedTab: 'tab1'
              });

             }}>
            <MyNavigatorIOS client={this.state.client} initialStep={this.state.initialStep} />

          </TabBarIOS.Item>

我正在尝试根据 react native docs 中的此示例使用该事件onPress触发。但是,不同之处在于我希望触发 TabBarIOS事件而不是子组件。我怎样才能做到这一点?this.props.navigator.popToTop();onPresspopToTop()MyNavigatorIOS

4

1 回答 1

2

我有同样的问题,你可以做的是添加一个 ref 到你的视图和它里面的导航器,像这样:

          this.refs.timeline.refs.timelineNavigator.popToTop()

所以 TabBar 代码如下所示:

    <TabBarIOS.Item
      systemIcon="history"
      title="Timeline"
      badge={this.state.timelineBadge}
      selected={this.state.selectedTab === 'timeline'}
      onPress={() => {
        if (this.state.selectedTab === 'timeline') {
          this.refs.timeline.refs.timelineNavigator.popToTop()
        } else {
          this.setState({ selectedTab: 'timeline' })
        }
      }}>
      <Timeline
        ref="timeline"
      /> 
    </TabBarIOS.Item>
于 2016-09-15T16:48:04.027 回答