Problems with UIRefreshControl

I am trying to implement UIRefreshControl in my application. I have an xib file, and I added a UITableViewController to an empty nib file, and I set the refresh property to "enabled". In addition, I added code to viewDidLoad and a custom update method. The problem is that I have an error. I canโ€™t find any information about .... in my viewDidLoad I get the 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 this property is not available .... what am I missing?

It looks like I need to inherit from the UITableViewController in my ViewController.h file. If I already have a UITableView , how do I inherit from both? If I change my code from ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> to ViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> , then I get the error message:

  error: NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "ViewController_iPhone" nib but didn't get a UITableView.' 
+6
source share
5 answers

You can add UIRefreshControl as a subtitle to your UITableView .

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

As with Dave, this could disrupt the future version of iOS. Therefore, please be careful when using this and try to raise an error report in apple regarding this.

Update: A better approach is to add a UITableViewController as a ChildViewController from self , and then add tableViewController.tableView as a sub-task of self.view . You do not need to make a hack for it to work this way.

 [self addChildViewController:tableViewController]; [self.view addSubview:tableViewController.tableView]; 

You can define a frame for tableView accordingly. Using this approach, the UIRefreshControl should work the same as for the UITableViewController . `

+11
source

What you need to remember:

  • UIRefreshControl only for UITableViewController , so your class must be a subclass of UITableViewController .

  • UITableViewController has refreshControl property, you must select UIRefreshControl and set it to this property.

Example:

 UITableViewController *tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain]; UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(refreshControlAction:) forControlEvents:UIControlEventValueChanged]; tableViewController.refreshControl = refreshControl; 
+5
source

These are all complicated ways to make something simple.

You do not need to add an update control or declare it in your viewController. Adding pull-to-refresh is a two-step process.
Step 1. In your storyboard, go to your TableViewController and, where it says โ€œUpdateโ€, select โ€œEnabledโ€.
Step 2. Add the following code to the tableViewController.m file, in viewDidLoad:

 [self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged]; 

This is the whole process, except what you do in your -refresh method. When you want to stop the update, call [self.refreshControl endRefreshing]; .

+2
source

Your ViewController class must be a subclass of UITableViewController in order to access the refreshControl property.

+1
source

I would recommend you make a separate subclass of UITableViewController for myTableView. And then use addChildviewController or iOS6 ContainerView to add this class to the original ViewController. Thus, even in the View part, you can use UIRefreshControl.

The accepted answer is not an official way, so it may break in a future version, as the comment said ...

0
source

Source: https://habr.com/ru/post/927276/


All Articles