문제

I have a UIRefreshControl in my ViewController and a method refreshView to handle "pull to refresh" event. It works perfectly when actually pulling, but when I call [refresh beginRefreshing] in code, it just shows refresh animation but doesn't call the refreshView method.

Here's code for initializing refresh control in 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;
도움이 되었습니까?

해결책

Based on my understanding on the documentation: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html

Tells the control that a refresh operation was started programmatically. ... Call this method when an external event source triggers a programmatic refresh of your table.

I think it is not used to start a refresh operation, rather it is just for updating the refresh control status that it is currently refreshing, in which it will make the refresh control spinning. The purpose will be to avoid the user pulling the table view and trigger the refresh operation again while it is still refreshing.

So you should call the refreshView: method by yourself.

다른 팁

Just call beginRefreshing asynchronously.

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

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

Try this

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;

Method

-(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

}

to stop it

[refreshControl endRefreshing];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top