首先找到自定义TabBar按钮的点击方法
//按钮被选中的事件
-(void) buttonSelected:(UIButton *) button{
self.lastBut.selected=NO;
button.selected=YES;
self.lastBut=button;
NSInteger index=[self.subviews indexOfObject:button];
if (index>2) {
index--;
}
//通过代理刷新viewcontroller
if ([self.delegate respondsToSelector:@selector(tabBarRefreshController:currentIndex:)]) {
[self.delegate tabBarRefreshController:self currentIndex:index];
}
}
接下来找到代理方法
//HQTabBarView当中的代理方法用来刷新数据
-(void)tabBarRefreshController:(HQTabBarView *)tabBarView currentIndex:(NSInteger)currentIndex{
//当tableBar被选中之后,发送一个通知
//在我们的精华控制器内接收该通知
NSMutableDictionary *info=[NSMutableDictionary dictionary];
info[HQTabBarControllerSelectionIndexKey]=[NSNumber numberWithInteger:currentIndex];
[[NSNotificationCenter defaultCenter] postNotificationName:HQTabBarControllerSelectedNotification object:nil userInfo:info];
self.selectedIndex=currentIndex;
}
在代理方法里发送了一个通知,然后去需要刷新的viewcontroller接收通知
-(void) tabBarClicked:(NSNotification *) notification{
NSDictionary *info=notification.userInfo;
//当前被选中的index
NSInteger currentIndex=[info[HQTabBarControllerSelectionIndexKey] integerValue];
self.clickCount++;
//判断是不是精华控制器+是不是当前窗口+是不是连续点击
if (currentIndex==0&&[self.view isShowingInWindow]&&self.clickCount>1) {
[self.tableView.mj_header beginRefreshing];
}
//如果点击了其他的控制器,将点击次数清零
if (currentIndex!=0) {
self.clickCount=0;
}
}
附判断一个view是否在当前窗口
#import "UIView+HQExtension.h"
@implementation UIView (HQExtension)
-(BOOL)isShowingInWindow{
//拿到主窗口
UIWindow *window=[UIApplication sharedApplication].keyWindow;
//转换坐标系
CGRect newFrame=[window convertRect:self.frame fromView:self.superview];
//判断当前view的frame是否在窗口内
BOOL isIn=CGRectIntersectsRect(newFrame, window.bounds);
return !self.hidden&&self.alpha>0.01&&self.window==window&&isIn;
}
@end