UIRefreshControl iOS 6 xcode

39,240

Solution 1

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.

Solution 2

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

Solution 3

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

}
Share:
39,240
Gareth
Author by

Gareth

Updated on July 09, 2022

Comments

  • Gareth
    Gareth almost 2 years

    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.

  • Gareth
    Gareth over 11 years
    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.
  • danqing
    danqing over 11 years
    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.
  • Dave DeLong
    Dave DeLong over 11 years
    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.
  • rjgonzo
    rjgonzo over 11 years
    Remember to call [self.refreshControl endRefreshing]; to stop the refresh control. You can also set a title. Cheers.
  • Dan Morrow
    Dan Morrow about 11 years
    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?
  • brainray
    brainray about 11 years
    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
  • DiscDev
    DiscDev over 10 years
    I had to add the UIRefreshControl as a subview of my UITableView to get this to work: [self.myTableView addSubview:refreshControl];
  • Avi Cohen
    Avi Cohen over 9 years
    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.