这篇文章主要介绍了iOS开发中TabBar再次点击实现刷新效果,实现方法也很简单,需要的朋友可以参考下
需求
之前已经实现了自定义TabBar,如图所示:
自定义TabBar.jpeg
现在需要实现一个类似今日头条TabBar的功能 —— 如果继续点击当前TabBar的选中项,那么该界面需要刷新UITableView。
分析
既然已经自定义了TabBar,那么最简单的就是在自定义中给TabBar中需要的UITabBarButton添加事件 —— 点击就发送通知,并且将当前的索引传出去。对应的界面监听通知,拿到索引比对,如果和当前索引一致,就执行对应的操作。
实现
1. 自定义TabBar的layoutSubviews中绑定事件
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIButton * tabBarButton in self.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
//监听tabbar的点击
//绑定tag 标识
tabBarButton.tag = index;
//监听tabbar的点击
[tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
}
2. 监听事件,发送通知
- (void)tabBarButtonClick:(UIControl *)tabBarBtn{
//判断当前按钮是否为上一个按钮
//再次点击同一个item时发送通知出去 对应的VC捕获并判断
if (self.previousClickedTag == tabBarBtn.tag) {
[[NSNotificationCenter defaultCenter] postNotificationName:
@"DoubleClickTabbarItemNotification" object:@(tabBarBtn.tag)];
}
self.previousClickedTag = tabBarBtn.tag;
}
对应的UIViewController监听通知
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doubleClickTab:) name:@"DoubleClickTabbarItemNotification" object:nil];
}
3. 监听到通知,比对后执行操作
-(void)doubleClickTab:(NSNotification *)notification{
//这里有个坑 就是直接用NSInteger接收会有问题 数字不对
//因为上个界面传过来的时候封装成了对象,所以用NSNumber接收后再取值
NSNumber *index = notification.object;
if ([index intValue] == 1) {
//刷新
}
}
最终的效果请看:
总结
以上所述是小编给大家介绍的iOS开发中TabBar再次点击实现刷新效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程学习网网站的支持!
织梦狗教程
本文标题为:iOS开发中TabBar再次点击实现刷新效果


基础教程推荐
猜你喜欢
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- Android中的webview监听每次URL变化实例 2023-01-23
- android studio按钮监听的5种方法实例详解 2023-01-12
- Flutter手势密码的实现示例(附demo) 2023-04-11
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- Android多返回栈技术 2023-04-15
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- IOS应用内跳转系统设置相关界面的方法 2022-11-20