6

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.'

5 Answers 5

11

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. `

5
  • 3
    This is unsupported and may break in future version of iOS. The only supported way to use a UIRefreshControl is with a UITableViewController. Oct 9, 2012 at 18:30
  • 1
    @ACB, that is exactly what I needed!!! Thanks a millions. I wish I could give you more props! Oct 9, 2012 at 18:30
  • 1
    Yes, you can have a UIRefreshControl on an UIViewController class with an UITableView and <UITableViewDelegate, UITableViewDataSource> that you added manually. The comments other answers may be true (it does not goes with the docs), but this little hack works on iOS6. Oct 18, 2012 at 17:54
  • @akaru The UIRefreshControl docs say: "You link a refresh control to a table through an associated table view controller object." Thus, other methods of adding a UIRefreshControl to a UITableView are unsupported. Jan 1, 2013 at 19:56
  • You should be using the UITableViewController.refreshControl property as per Narendra Pathai's answer. Manually adding it to the UITableView like this is dangerous and could break in future versions of iOS.
    – Samah
    Jan 3, 2016 at 22:06
5

Things to Remember:

  • UIRefreshControl only for UITableViewController, so your class should be the subclass of UITableViewController.

  • UITableViewController has a property refreshControl, you should allocate a UIRefreshControl and set it to that property.

Ex:

UITableViewController *tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];

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

tableViewController.refreshControl = refreshControl;
1
  • This is the correct answer. Manually adding it to the UITableView as per the accepted answer could cause unexpected behaviour in future versions of iOS.
    – Samah
    Jan 3, 2016 at 22:08
3

All of these are complex ways of doing something simple.

You don't need to add a refresh control, or declare one 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 "Refreshing", select "Enabled".
Step 2: Add the following code to your tableViewController.m file, in viewDidLoad:

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

That's the entire process, other than doing stuff in your -refresh method. When you want it to stop refreshing, call [self.refreshControl endRefreshing];.

1
  • Thanks! I had a feeling all the tutorials I was reading were overcomplicated. Dec 11, 2013 at 5:38
1

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

4
  • We can add UITableViewController as a child view controller and then add tableview as a subview of self.view of current view controller, right? I was just wondering whether we really need to subclass?
    – iDev
    Jan 22, 2013 at 19:42
  • @ACB Yes, you can use a UITableViewController as a child view controller. That is an excellent approach. Jan 22, 2013 at 23:07
  • Thanks for confirming. I was confused with this statement in your answer "ViewController class must be a subclass of UITableViewController" since we can add it as a subview as well.
    – iDev
    Jan 22, 2013 at 23:18
  • @ACB that statement is meant to be interpreted in the context of the question above. :) Jan 22, 2013 at 23:51
0

I would recommend you to make separate UITableViewController Subclass for myTableView. And then by using addChildviewController or iOS6 ContainerView to add that class within original ViewController. That way even in the part of View, you can use UIRefreshControl.

Accepted answer is not official way, so it could break in future release as comment said...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.