本节将实现重新浪的接口获取到用户的未读消息数,并显示在底部的Tabbar上,经过定时器每隔几秒请求新浪的接口,而后将得到的各类消息数经过badgeValue显示出来。git
在Home(首页)/Model/userUnreadCount/目录下写模型github
代码:
请求参数ui
// 请求参数继承自IWBaseParam // IWUserUnreadCountParam.h // ItcastWeibo // // Created by kaiyi on 16-6-18. // Copyright (c) 2016年 itcast. All rights reserved. // #import "IWBaseParam.h" @interface IWUserUnreadCountParam : IWBaseParam /** * 须要查询的用户ID。 */ @property (nonatomic, strong) NSNumber *uid; @end
返回参数数据模型atom
// 返回参数数据模型 // IWUserUnreadCountResutl.h // ItcastWeibo // // Created by kaiyi on 16-6-18. // Copyright (c) 2016年 itcast. All rights reserved. // #import <Foundation/Foundation.h> @interface IWUserUnreadCountResutl : NSObject // 新微薄未读数 @property (nonatomic, assign) int status; // 新粉丝数 @property (nonatomic, assign) int follower; // cmt @property (nonatomic, assign) int cmt; // 新私信数 @property (nonatomic, assign) int dm; // 新通知未读数 @property (nonatomic, assign) int notice; // 新说起个人微博数 @property (nonatomic, assign) int mention_status; // 新说起个人评论数 @property (nonatomic, assign) int mention_cmt; // 未读消息的总数 -(int)messageCount; @end
在Main(主要)/Controller/IWTabBarViewController.m目录中发送请求spa
- (void)viewDidLoad { [super viewDidLoad]; // 初始化tabbar [self setupTabbar]; // 初始化全部的子控制器 [self setupAllChildViewControllers]; [self checkUnreadCount]; // 定时检查未读数 [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(checkUnreadCount) userInfo:nil repeats:YES]; } // 定时检查未读数 -(void)checkUnreadCount { // 1.请求参数 IWUserUnreadCountParam *param = [IWUserUnreadCountParam param]; param.uid = @([IWAccountTool account].uid); // 2.发送请求 [IWUserTool userUnreadCountWithParam:param success:^(IWUserUnreadCountResutl *result) { // 3.设置badgeValue // 3.1首页 self.home.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", result.status]; self.message.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", result.messageCount]; self.me.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", result.follower]; } failure:^(NSError *error) { }]; }
经过下拉刷新,清除提醒数字:
Home(首页)/Controller/IWHomeViewController.mcode
/** * 刷新数据(向新浪获取更新的微博数据) */ - (void)loadNewData { // 0.清除提醒数据 self.tabBarItem.badgeValue = nil; // 1.封装请求参数 // 其余代码省略 }
动态图:orm
项目源代码,请看Github:微博开发项目源代码blog