I am trying to implement the UIRefreshControl
in my application. I have an xib file and I added a UITableViewController
to the empty nib file and I set the refresh property to "enabled". Also I have added code to the viewDidLoad
and a custom refresh method. The problem is I have an error I can't find any information on....in my viewDidLoad
I get "Property 'refreshControl
' not found on object of type ViewController
"
- (void)viewDidLoad{
[super viewDidLoad];
self.myTableView =
[[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStyleGrouped];
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
[self.view addSubview:self.myTableView];
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[refresh addTarget:self action:@selector(refreshView:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
}
-(void)refreshView:(UIRefreshControl *)refresh {
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing data..."];
// custom refresh logic would be placed here...
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM d, h:mm a"];
NSString *lastUpdated = [NSString stringWithFormat:@"Last updated on %@",
[formatter stringFromDate:[NSDate date]]];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];
[refresh endRefreshing];
}
I have no idea why that property isn't available....what am I missing?
Looks like I need to inherit from UITableViewController
in my ViewController.h
file. If I already have UITableView
there how do I inherit from both? If I change my code from ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
to ViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
then I get an error:
error: NSInternalInconsistencyException',
reason: '-[UITableViewController loadView] loaded the "ViewController_iPhone" nib but didn't get a UITableView.'
You can add UIRefreshControl
as a subview to your UITableView
.
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[self.myTableView addSubview:refreshControl];
As per Dave's comment, this may break in future version of iOS. So please be careful while using this and try to raise a bug report to apple regarding this.
Update:
A better approach is by adding UITableViewController
as a ChildViewController
of self
and then adding tableViewController.tableView
as the subview of self.view
. You dont have to do any hack to make it work in this way.
[self addChildViewController:tableViewController];
[self.view addSubview:tableViewController.tableView];
You can define the frame for tableView
accordingly. Using this approach, UIRefreshControl
should work in the same way as it works for UITableViewController
.
`