tabbar双击刷新

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/PianZhideNanRen/article/details/79446411

最近不太忙,就开始对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;
}
刷新数据我使用的是基类方法,大家也可以使用通知进行刷新数据;


猜你喜欢

转载自blog.csdn.net/PianZhideNanRen/article/details/79446411
今日推荐