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.
问题:
回答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.
回答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];
}
回答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];
}