سؤال

I am building an App that needs to be able to refresh by pulling, and then re-run the ViewDidLoad. So I know i could use a UIRefreshControl, but I can only find code that is used in a UITableViewController. Has anyone got an idea on how to use a UIRefreshControl in a UIView instead of in a UITableView. Thanks in advance.

هل كانت مفيدة؟

المحلول 3

UIRefreshControl isn't meant to be used without a table. This note in the documentation specifically warns against such use:

Note: Because the refresh control is specifically designed for use in a table view that's managed by a table view controller, using it in a different context can result in undefined behavior.

So although it may be possible to get the control to work, you really shouldn't use it without a table. Rolling your own refresh control is a better solution.

Better still, try to engineer your app such that the user doesn't need to refresh the view. The app should know when new data is available. The only excuse for making the user refresh is if doing so automatically would somehow confuse the user or otherwise make the app more difficult to use.

نصائح أخرى

try this…

[myView setNeedsDisplay];

this would reload your view controller. you can put this in a method and call it during pull to refresh…Hope this helps you. Happy coding ;)

Very easily: Keep the control as amember or property:

UIRefreshControl *_refreshControl;

Add this to your viewDidLoad method: (simply adding the control to your tableView. make sure the tableView is not nil ofcourse).

 _refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[_tableView insertSubview:_refreshControl atIndex:0];

implement the refresh method:

- (void)refresh:(UIRefreshControl *)refreshControl {
   [self reloadData];
    //Don't forget to stop the refreshing animation after data reloads.
  [_refreshControl endRefreshing];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top