3
votes

I have a UIView with a connected UIViewController. As a subview (built via the interface builder), I have a UITableView. I would like to add a refresh control to that UITableView, but to do that I need to get the default controller behind the table view.

Is there a method of UITableView that I can get the UITableViewController it is associated with from?

1
There is a confusion between UIView and UIViewController. A UITableView doesn't have a UITableViewController instance associated to it. It just have a delegate and a data source, which are often a view controller. UITableViewController is just a convenience class for less setup of your UIViewController. But it remains essentially a UIViewController.PatrickNLT

1 Answers

3
votes

Unfortunately, there is not a way to access the UITableView's controller without subclassing and setting a delegate after instantiation (but I highly, highly recommend against this). Your best bet would be to set the refresh control within the UITableViewController (in code, if possible) that you've linked up to the table view in IB.

To add a refresh control to a UITableView that is not already being controlled by a UITableViewController, you can do the following.

First, create a UITableViewController to manage the control.

UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = self.tableView;

Create the control, add a target that manages the UITableView's state after a refresh, and then assign it to the tableViewController you just created.

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

The final thing to do is to implement refreshControlValueChanged: in your UIViewController.