AppDelegate.m文件
@interface AppDelegate ()<UITabBarControllerDelegate>
//监听tabar点击事件,(DXDTabat.m代替之)
#pragma mark - <UITabBarControllerDelegate>
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// 发出一个通知
[ [NSNotificationCenter defaultCenter] postNotificationName:@"DXDTabBarDidSelectNotification" object:nil userInfo:nil];
}
//在需要刷新的ViewController.m文件
// 监听tabbar点击的通知
- (void)viewDidLoad {
[DXDNoteCenter addObserver:self selector:@selector(tabBarSelect) name:DXDTabBarDidSelectNotification object:nil];
}
- (void)tabBarSelect
{
// 如果是连续选中2次, 直接刷新
if (self.lastSelectedIndex == self.tabBarController.selectedIndex
// && self.tabBarController.selectedViewController == self.navigationController
&& [self isShowingOnKeyWindow]) {
[self.tableView.header beginRefreshing];
}
// 记录这一次选中的索引
self.lastSelectedIndex = self.tabBarController.selectedIndex;
}
- (BOOL)isShowingOnKeyWindow
{
// 主窗口
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
// 以主窗口左上角为坐标原点, 计算self的矩形框
CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
CGRect winBounds = keyWindow.bounds;
// 主窗口的bounds 和 self的矩形框 是否有重叠
BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
}