Get UITableViewController from UITableView

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

Is there a UITableView way that I can get the UITableViewController it is associated with?

+3


source to share


1 answer


Unfortunately there is no way to access the UITableView controller without subclassing and setting the delegate after instantiation (but I highly recommend against that). Your best bet would be to set the update control to the UITableViewController (in code if possible) that you linked to the table view in IB.

To add an update control to the UITableView that is not yet controlled by the UITableViewController, you can do the following.

First, create a UITableViewController to control the control.

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

      



Create a control, add a target that controls the state of the UITableView after refresh, and then assign it to just the tableViewController you create.

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

      

The last thing to do is implement it refreshControlValueChanged:

in your UIViewController.

+3


source







All Articles