Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIRefreshControl issues

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.'
like image 243
brianhevans Avatar asked Oct 09 '12 17:10

brianhevans


4 Answers

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

like image 140
iDev Avatar answered Sep 17 '22 15:09

iDev


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;
like image 45
lara1435 Avatar answered Sep 21 '22 15:09

lara1435


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];.

like image 26
AMayes Avatar answered Sep 19 '22 15:09

AMayes


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

like image 41
Dave DeLong Avatar answered Sep 21 '22 15:09

Dave DeLong