42

Does anyone have a short example of how to implement the new UIRefreshControl into xcode. I have a UITableViewController which displays Tweets, want to be able to pull down and refresh.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Gareth
  • 512
  • 1
  • 4
  • 15
  • Looks like you're expecting something similar to this: http://www.lextech.com/2012/10/ios-6-pull-to-refresh-tutorial/ In the new OS we're able to render similar/same as the OOTB Pull to Refresh scenario. – Mike Davis Jan 07 '13 at 21:48

3 Answers3

68

You can just set it up in your viewDidLoad, if you have a UITableViewController:

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(refresh)
         forControlEvents:UIControlEventValueChanged]; 
self.refreshControl = refreshControl;

Then you can do your refresh stuff here:

-(void)refresh {
    // do something here to refresh.
}

When you are done with refreshing, call [self.refreshControl endRefreshing]; to stop the refresh control, as pointed out by rjgonzo.

danqing
  • 3,348
  • 2
  • 27
  • 43
  • thanks iBlue!! within the (void) refresh what would I need to put, sorry i'm very new to this and the apple documentation is a bit complex for me. – Gareth Sep 26 '12 at 17:26
  • if you are rather using a button to refresh, not a refresh controller, how you are gonna handle it? Just put **that** code in the `refresh` method. Nothing fancy. – danqing Sep 26 '12 at 17:31
  • 17
    Remember to call `[self.refreshControl endRefreshing];` to stop the refresh control. You can also set a [title](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html). Cheers. – rjgonzo Dec 10 '12 at 01:54
  • 1
    I had to add the UIRefreshControl as a subview of my UITableView to get this to work: [self.myTableView addSubview:refreshControl]; – DiscDev Sep 16 '13 at 19:28
32

You can also configure this in Interface Builder. Though the way it currently works, it only saves you a couple of lines of code.

Select the TableViewController scene and in the Attributes Inspector, you'll find a drop down list option for "Refreshing." Set that to "Enabled." You'll notice in the View Controller Hierarchy that a "Refresh Control" has been added (you won't see anything visually added to the scene itself). What's odd is that after hooking up the Refresh Control to an IBAction (Value Change Event) the event doesn't seem to get triggered. I'm guessing that's a bug (?) but meanwhile, setting "Refreshing" to enabled creates the UIRefreshControl object and assigns it to the view controller's refreshControl property. With that done, you can add the event handling line to your viewDidLoad method:

[self.refreshControl addTarget:self action:@selector(refreshView:) forControlEvents:UIControlEventValueChanged];

In your refreshView: method, you can do some work and then stop the refresh animation:

- (void)refreshView:(UIRefreshControl *)sender {
    // Do something...
    [sender endRefreshing];
}
J Shapiro
  • 3,861
  • 1
  • 19
  • 29
  • FYI, you may not want to call `-endRefreshing` in the action handler, because then your `UIRefreshControl` will *immediately* animate out after doing the "snap" animation. – Dave DeLong Oct 07 '12 at 19:03
  • 1
    about the "the event doesn't seem to get triggered" part. Any idea why that is? It looks like I can add this in InterfaceBuilder, but what's the point if this doesn't work? It's not much code to add in, but this seems really odd. Ever find out why this is? – Dan Morrow Mar 04 '13 at 16:12
  • 2
    The "Refreshing" item is only there, if you have pulled a TableViewController out object from the list. It's not thee, if you constructed your View by yourself. SO, don't wonder if it's not there – brainray Apr 07 '13 at 10:25
  • 2
    On XCode 6 the IB bug is fixed. Now in IB, you can control drag from the Refresh Control to the code to create an IBAction. – Avi Cohen Sep 20 '14 at 11:10
18

Here how you do pull Down and Refresh

In your UITableViewController.h add UIRefreshControl *refreshControl; and -(void) refreshMyTableView; method global declaration

and in viewDidLoad of UITableViewController.m

//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;

and refresh function with date and time of refresh

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

}
iMeMyself
  • 1,649
  • 13
  • 28