如何监听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
- 如何监听Tabbar的点击
- 监听TabBar点击事件 使系统tabar的item 点击刷新页面 swift版
- 如何监听虚拟键盘的点击事件
- Tabbar点击后如何回到初始页
- 自定义TabBar如何实现连续点击刷新
- 点击tabBar返回到当前tabBar的初始视图
- IOS点击底部tabbar的事件
- 28.如何监听键盘最右下角按钮的点击
- RecyclerView的使用方法与如何设置点击监听
- radioGroup的点击监听
- 自定义的tabbar在跳转之后隐藏tabbar后,tabbar原来的位置不能响应点击事件,这样解决
- 监听按钮的点击事件
- 监听按钮的点击事件
- 监听按钮的点击事件
- 监听按钮的点击事件
- 监听按钮的点击事件
- android ImageView点击事件如何监听
- tabbar点击刷新
- hdu1025 Constructing Roads In JGShining's Kingdom
- 软件开发注意事项
- 2016年5月20日总结
- 软件设计师考试2
- table 行上移 下移 置顶 删除
- 如何监听Tabbar的点击
- 信息框出现位置的理解
- spark分布式安装 spark集群搭建 hadoop集群搭建
- 一个最简单的Servlet服务
- Java虚拟机模型
- 简单的文件处理
- jetson tk1 上面如何编译opencv程序
- Android之ListView内容数据刷新
- 实习入职第二天:maxMemory()--totalMemory()--freeMemory()