在prorgammatically调用时,UIRefreshControl无法正常工作

时间:2013-05-23 15:49:59

标签: ios pull-to-refresh uirefreshcontrol

我的UIRefreshControl中有一个ViewController,方法refreshView处理“拉到刷新”事件。它在实际拉动时效果很好,但是当我在代码中调用[refresh beginRefreshing]时,它只显示刷新动画但不调用refreshView方法。

以下是在viewDidload中初始化刷新控制的代码:

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[refresh addTarget:self
            action:@selector(refreshView:)
  forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;

3 个答案:

答案 0 :(得分:14)

根据我对文档的理解:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html

  

告诉控件已启动刷新操作   编程。 ...在外部事件时调用此方法   source会触发对表的程序化刷新。

我认为它不用于启动刷新操作,而只是用于更新当前正在刷新的刷新控制状态,其中它将使刷新控制旋转。目的是避免用户拉出表视图并在仍然刷新时再次触发刷新操作。

所以你应该自己调用refreshView:方法。

答案 1 :(得分:0)

只需异步调用beginRefreshing。

- (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view.

   dispatch_async(dispatch_get_main_queue(), ^{
       [refreshControl beginRefreshing];
   });
   //...
}

答案 2 :(得分:-1)

试试这个

viewDidLoad中

//initialise the refresh controller
refreshControl = [[UIRefreshControl alloc] init];
//set the title for pull request
refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"pull to Refresh"];
//call he refresh function
[refreshControl addTarget:self action:@selector(refreshMyTableView)
         forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;

方法

-(void)refreshMyTableView{

    //set the title while refreshing
    refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing the TableView"];
    //set the date and time of refreshing
    NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
    [formattedDate setDateFormat:@"MMM d, h:mm a"];
    NSString *lastupdated = [NSString stringWithFormat:@"Last Updated on %@",[formattedDate stringFromDate:[NSDate date]]];
    refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:lastupdated];
    //end the refreshing

}

停止它

[refreshControl endRefreshing];