iPhone开发笔记(19)实现类似网易新闻当应用状态变为active时手动调用下拉刷新的功能

来源:互联网 发布:mac的launchpad在哪里 编辑:程序博客网 时间:2024/06/14 07:29

    在使用iOS应用的用户中,很大一部分用户极少会有双击home键关闭不用的进程的习惯。由此引出了一个问题,那就是当用户不使用时按home键将程序挂起。如果该应用使用了本地缓存机制,过了一段时间再打开应用时还是上一次的内容。关于这个问题,网易新闻的解决方案是当应用从后台运行转为前台时自动调用下拉刷新来更新数据。下面就谈谈这种功能的实现方法。

    1、首先在相应的viewController的viewDidLoad方法里面添加一个通知,具体代码如下

[plain] view plaincopy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     //响应程序从后台转为前台的通知  
  6.     [[NSNotificationCenter defaultCenter] addObserver:self  
  7.                                              selector:@selector(updatePictureNewsByActive)  
  8.                                                  name:UIApplicationDidBecomeActiveNotification object:nil];  
  9. }  


    2、在这个viewController的dealloc方法中添加移除通知的代码

[plain] view plaincopy
  1. - (void)dealloc  
  2. {  
  3.     [super dealloc];  
  4.     [[NSNotificationCenter defaultCenter] removeObserver:self];  
  5. }  


    3、这样当应用从后台转为active时候就会调用updatePictureNewsByActive方法更新数据了,在updatePictureNewsByActive方法中通过手动调用下拉刷新的代码如下:

[plain] view plaincopy
  1. - (void)updatePictureNewsByActive  
  2. {      
  3.     [self.tableView setContentOffset:CGPointMake(0, -75) animated:YES];  
  4.     [self performSelector:@selector(doneManualRefresh) withObject:nil afterDelay:0.3];  
  5. }  
  6.   
  7. - (void)doneManualRefresh  
  8. {  
  9.     [refreshTableHeaderView egoRefreshScrollViewDidScroll:self.tableView];  
  10.     [refreshTableHeaderView egoRefreshScrollViewDidEndDragging:self.tableView];  
  11. }  
     我这里使用的是EGOTableViewPullRefresh的开源类库实现的下拉刷新,关于它的使用方法在我的文章iPhone开发笔记(17)使用EGOTableViewPullRefresh实现列表的下拉刷新功能中有详细的介绍。
0 0
原创粉丝点击