Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UITableViewController from UITableView

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?

like image 480
steventnorris Avatar asked May 18 '26 00:05

steventnorris


1 Answers

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.

like image 198
dwlz Avatar answered May 20 '26 23:05

dwlz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!