如何监听Tabbar的点击

来源:互联网 发布:无创dna数据看性别图 编辑:程序博客网 时间:2024/06/07 10:21

如何监听Tabbar的点击

如果不是自定义的Tabbar

  • 实现UITabBarController的代理方法tabBarController: didSelectViewController:, 每次Tabbar被点击了都会来到这个代理方法
  • 在这个代理方法中发送通知
  • 在需要监听Tabbar点击的控制器中监听上面发出的通知
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{      [[NSNotificationCenter defaultCenter] postNotificationName:AHTabBarDidSelectNotification object:nil userInfo:nil];}
// 监听通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabBarSelect) name:AHTabBarDidSelectNotification object:nil];
- (void)tabBarSelect{// 如果是连续选中2次, 直接刷新if(self.lastSelectedIndex == self.tabBarController.selectedIndex && self.view.isShowingOnKeyWindow){    [self.tableView.mj_header beginRefreshing];}self.lastSelectedIndex = self.tabBarController.selectedIndex;}

如果是自定义的Tabbar

  • 在自定义的Tabbar中, 给Tabbar中的UITabBarButton添加Target, 每次UITabBarButton一点击, 就发送通知, 剩下的基本和上面一样

自定义的tabbar

  • .h
////  AHTabBar.h//  01-百思不得其姐////  Created by AHUAN on 16/3/2.//  Copyright © 2016年 AHUAN. All rights reserved.//#import <UIKit/UIKit.h>@interface AHTabBar : UITabBar@end
  • .m
#import "AHTabBar.h"#import "AHPublishView.h"@interface AHTabBar()/** 加按钮 */@property (strong, nonatomic) UIButton *publishButton;@end@implementation AHTabBar- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self){        [self setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon_click"] forState:UIControlStateHighlighted];        publishButton.size = publishButton.currentBackgroundImage.size;        [publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];        self.publishButton = publishButton;        [self addSubview:publishButton];    }    return self;}- (void)layoutSubviews{    [super layoutSubviews];    // 标记按钮是否已经添加过监听器    static BOOL added = NO;    CGFloat width = self.width;    CGFloat height = self.height;    self.publishButton.center = CGPointMake(width * 0.5, height * 0.5);    CGFloat buttonY = 0;    CGFloat buttonW = width / 5;    CGFloat buttonH = height;    NSInteger index = 0;    for(UIControl *button in self.subviews){        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;        // 计算按钮的x值        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);        // 增加索引        index++;        if(added == NO){            [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];        }    }    added = YES;}- (void)buttonClick{    [[NSNotificationCenter defaultCenter] postNotificationName:AHTabBarDidSelectNotification object:nil userInfo:nil];}- (void)publishClick{    [AHPublishView show];}@end
0 0
原创粉丝点击