Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIRefreshControl iOS 6 xcode

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.

like image 976
Gareth Avatar asked Sep 26 '12 17:09

Gareth


3 Answers

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.

like image 50
danqing Avatar answered Nov 19 '22 23:11

danqing


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];
}
like image 37
J Shapiro Avatar answered Nov 20 '22 00:11

J Shapiro


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

}
like image 18
iMeMyself Avatar answered Nov 19 '22 23:11

iMeMyself