m0_62833091 2025-04-14 08:20 采纳率: 88.4%
浏览 5
已结题

双角色tabbar点击按钮切换模拟器和真机不一样??

双角色tabbar,模拟器上面正常,真机上的问题:从首页选择论坛角色进入,在我的页面点击去社区按钮,页面成功跳转,tabbar没有切换,显示的是论坛角色tabbar导航栏,如果从首页选择社区角色进入,在我的页面点击去论坛按钮,页面成功跳转,tabbar导航栏还是社区角色tabbar导航栏,这是什么问题,怎么解决?

img

<!--custom-tab-bar/index.wxml-->
<view class="tab-bar">
    <view class="tab-bar-item" wx:for="{{selectList}}" wx:key="index" data-index="{{index}}" data-path="{{item.pagePath}}" data-selected="{{item.selected}}" bindtap="onwidthTap">
        <image class="cover-image" src="{{selected === item.selected ? item.selectedIconPath : item.iconPath}}"></image>
        <view class="cover-view {{selected === item.selected ? 'active' : ''}}" >{{item.text}}</view>
    </view>
</view>
<view class="indicator-border"></view>
<view class="indicator">
    <view class="indicator-item">
        <image class="indicator-image" src="/images/jia.png"></image>
        <view class="indicator-item-text">发布</view>
    </view>   
</view>
// custom-tab-bar/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    allList: [
      [{
          "pagePath""/pages/jobs/index/index",
          "iconPath""/images/me-gray.png",
          "selectedIconPath""/images/me-green.png",
          "text""论坛",
          "selected""0"
        },
        {},
        {
          "pagePath""/pages/jobs/me/me",
          "iconPath""/images/me-gray.png",
          "selectedIconPath""/images/me-green.png",
          "text""我的",
          "selected""1"
        }
      ],
      [{
          "pagePath""/pages/enterprise/index/index",
          "iconPath""/images/me-gray.png",
          "selectedIconPath""/images/me-green.png",
          "text""社区",
          "selected""2"
        },
        {},
        {
          "pagePath""/pages/enterprise/me/me",
          "iconPath""/images/me-gray.png",
          "selectedIconPath""/images/me-green.png",
          "text""我的",
          "selected""3"
        }
      ]
    ],
    selectedList: []
  },
/**
   * 生命周期的方法:当组件被加载的时候调用
   */
  
  lifetimes:{
    attached(){
      if(wx.getStorageSync('user') == '0'){
        this.setData({
          selectList:this.data.allList[0]
        })
      }else{
        this.setData({
          selectList:this.data.allList[1]
        })
      }
    },
    show(){
      this.updateTabbar();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onwidthTap(e) {
      console.log(e);
      let path = e.currentTarget.dataset.path;
      let selected = e.currentTarget.dataset.selected;
      wx.switchTab({
        url:path
      })
    },
    updateTabbar(){
      const userType = wx.getStorageSync('user') || '0';
      this.setData({
        selectList:this.data.allList(userType)
      })
    }
  }
})
// custom-tab-bar/index.js
Component({
  data: {
    allList: [
      [{
          "pagePath""/pages/jobs/index/index",
          "iconPath""/images/me-gray.png",
          "selectedIconPath""/images/me-green.png",
          "text""论坛",
          "selected""0"
        },
        {},
        {
          "pagePath""/pages/jobs/me/me",
          "iconPath""/images/me-gray.png",
          "selectedIconPath""/images/me-green.png",
          "text""我的",
          "selected""1"
        }
      ],
      [{
          "pagePath""/pages/enterprise/index/index",
          "iconPath""/images/me-gray.png",
          "selectedIconPath""/images/me-green.png",
          "text""社区",
          "selected""2"
        },
        {},
        {
          "pagePath""/pages/enterprise/me/me",
          "iconPath""/images/me-gray.png",
          "selectedIconPath""/images/me-green.png",
          "text""我的",
          "selected""3"
        }
      ]
    ],
    selectedList: []
  },
  lifetimes:{
    attached(){
      if(wx.getStorageSync('user') == '0'){
        this.setData({
          selectList:this.data.allList[0]
        })
      }else{
        this.setData({
          selectList:this.data.allList[1]
        })
      }
    },
    show(){
      this.updateTabbar();
    }
  },
  methods: {
    onwidthTap(e) {
      console.log(e);
      let path = e.currentTarget.dataset.path;
      let selected = e.currentTarget.dataset.selected;
      wx.switchTab({
        url:path
      })
    },
    updateTabbar(){
      const userType = wx.getStorageSync('user') || '0';
      this.setData({
        selectList:this.data.allList(userType)
      })
    }
  }
})
<!--pages/index/index.wxml-->
<view class="title">
    <view class="jobs-group">
        <image class="jobs-img" src="../../images/jobs.jpeg"></image>
        <button class="jobs" bindtap="goto" data-type="0">论坛</button>
    </view>
    <view class="separate"></view>
    <view class="jobs-group">
        <image class="jobs-img" src="../../images/enterprise.jpg"></image>
        <button class="jobs" bindtap="goto" data-type="1">社区</button>
    </view>    
</view>
// pages/index/index.js
Page({
  goto(e){
    console.log(e);
    let type = e.currentTarget.dataset.type;
    //把权限存储到缓存当中
    wx.setStorageSync('user',type);
    if(type == "0"){
      wx.switchTab({
        url:'../jobs/index/index'
      })
    }else{
      wx.switchTab({
        url:'../enterprise/index/index'
      })
    }
  },
})
<!--pages/jobs/me/me.wxml-->
<view class="switch-group" bindtap="onSwitch">
    <image class="switch-image" src="../../../images/switch.png"></image>
    <view class="switch-text">去社区</view>
</view>
// pages/jobs/me/me.js
Page({
  onSwitch(){
    wx.setStorageSync('user','1');
    wx.switchTab({
      url:'../../enterprise/index/index'
    });
    // 强制更新自定义tabbar(需配合组件改造)
    const tabbar = this.getTabBar(); 
    tabbar.setData({ selectList: tabbar.data.allList[0] });
  },
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected"1"
      })
    }
  },
})
<view class="switch-group" bindtap="onSwitchEvent">
    <image class="switch-image" src="../../../images/switch.png"></image>
    <view class="switch-text">去论坛</view>
</view>
<!--pages/enterprise/me/me.wxml-->
<view class="switch-group" bindtap="onSwitchEvent">
    <image class="switch-image" src="../../../images/switch.png"></image>
    <view class="switch-text">去论坛</view>
</view>
<view class="text">社区页</view>
// pages/enterprise/me/me.js
Page({
  onSwitchEvent(){
    wx.setStorageSync('user','0');
    wx.switchTab({
      url:'../../jobs/index/index'
    });
    //强制更新自定义tabbar(需配合组件改造)
    const tabbar = this.getTabBar(); 
    tabbar.setData({ selectList: tabbar.data.allList[1] });
  },
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected"3"
      })
    }
  },
})
  • 写回答

3条回答 默认 最新

  • 紫雾凌寒 新星创作者: 人工智能技术领域 2025-04-14 08:21
    关注

    让 “紫雾凌寒” 来帮你回答,本回答结合 DeepSeek

    如果还有问题,请评论或点击头像私信,如果有帮助到您, 请关注、采纳、点赞

    1. 关键点分析:
      - 问题主要出现在真机上,从不同角色进入应用后,在特定页面点击按钮跳转时,tabbar没有正确切换,而是显示上一个角色的tabbar导航栏。这可能涉及到页面跳转与tabbar切换逻辑的不一致,以及不同设备环境下的适配问题。
    2. 解决方案:
      - 首先检查页面跳转和tabbar切换的代码逻辑。例如,在点击“去社区按钮”的事件处理函数中,确保正确地触发了tabbar切换的操作。
      - 假设使用的是iOS开发,以Objective - C为例,在按钮点击事件中可能类似这样的代码:
      ```objective - c
      - (IBAction)goToCommunityButtonTapped:(id)sender {
      // 跳转到社区页面
      CommunityViewController *communityVC = [[CommunityViewController alloc] init];
      [self.navigationController pushViewController:communityVC animated:YES];
      // 切换tabbar
      self.tabBarController.selectedIndex = 1; // 假设社区页面对应的tabbar索引是1
      }
       - 如果是在真机上出现问题要注意真机的设备特性和系统版本等因素比如某些系统版本可能对tabbar切换有特殊的处理要求可以添加一些调试代码在真机上打印相关的状态信息如当前选中的tabbar索引等以便定位问题
       - 另外确保在不同角色进入应用时对tabbar的初始化和状态设置是正确的例如在应用启动时根据初始进入的角色设置正确的tabbar初始选中项
    ```objective - c
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // 假设通过某个标志判断初始角色
        if (isForumRole) {
            self.tabBarController.selectedIndex = 0; // 论坛角色对应的tabbar索引
        } else {
            self.tabBarController.selectedIndex = 1; // 社区角色对应的tabbar索引
        }
        return YES;
    }
    
    1. 不同解决方案优缺点:
      - 代码检查方案
      • 优点:直接针对问题的核心代码逻辑进行排查,能够快速定位到可能出现问题的代码行,针对性强。
      • 缺点:需要对相关的页面跳转和tabbar切换代码非常熟悉,可能需要花费一定时间仔细检查代码细节。
      • 调试打印方案
      • 优点:可以直观地获取运行时的状态信息,帮助快速判断问题出现在哪个环节,尤其是在真机环境下能更准确地反映实际情况。
      • 缺点:调试代码添加和移除比较麻烦,如果没有及时清理,可能会影响代码的整洁性和性能。
    2. 总结:
      - 该问题主要是真机上tabbar切换异常,需要检查页面跳转与tabbar切换的代码逻辑,尤其是在不同角色进入应用的场景下。通过代码检查和调试打印等方式定位问题,确保tabbar在不同角色进入和页面跳转时能正确切换显示。

    希望以上解答对您有所帮助。如果您有任何疑问,欢迎在评论区提出。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月12日
  • 已采纳回答 7月4日
  • 创建了问题 4月14日