tabbar双击刷新

最近不太忙,就开始对APP进行优化,看到很多APP都增加的tabbar双击一个item刷新当前列表;

我的大概思路就是保存选中的索引,在进行二次判断,保存的记录和当前选中的相等则刷新当前界面的数据。

上代码

@interface MainTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic, assign) NSInteger currentSelectedIndex;  //当前选中的tabbar索引 默认-1
@end
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    #pragma mark - 双击tabbarItem刷新列表
    //1.设置默认值以及绑定代理
    self.currentSelectedIndex = -1;
    self.delegate = self; //也可以使用KVO监听当前TabBarController的selectedIndex属性和代理协议实现相同的监听
    
    [self buildView];
}
#pragma mark - UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
    NSLog(@"11111selectedIndex:%lu", (unsigned long)tabBarController.selectedIndex);
    
    //2.判断是否需要刷新
    if (tabBarController.selectedIndex == self.currentSelectedIndex) { //是否第二次点击
        if ([viewController isKindOfClass:[UINavigationController class]]) {    //rootController是否是导航条
            UIViewController *baseViewController = ((UINavigationController *)viewController).topViewController;
            if ([baseViewController isKindOfClass:[BaseViewController class]]) {    //是否是基类
                
                //3.刷新列表
                [(BaseViewController *)baseViewController base_reloadListData];
            }
        }
    }
    
    //4.记录当前选中tabbarItem的索引
    self.currentSelectedIndex = tabBarController.selectedIndex;
}
刷新数据我使用的是基类方法,大家也可以使用通知进行刷新数据;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值