I have a webpage
which has a long list of items. I want the user to refresh the page when they tapped on the tab bar item. I manage to call the method to load the web view when they tapped the tab bar item. But it just doesn't reload afresh from the top. It seems like webview
is loading from where it is or cache or something. I tried to removecahce
but it doesn't seems to work. I want it to reload afresh from the top again. I can't use viewWillAppear
because I don't want the page to reload when it is back from the detail
page. And I also want the page to reload when the tab bar item is being tapped.
Reload at where you browse, I need to reload afresh from all the way from the top.
MyTabBarController.m
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if([viewController isKindOfClass:[ClassNavigationController class]]) {
Classes *myClasses = [[Classes alloc] init];
[myClasses reloadWebView];
}
}
Classes.m
- (void)LoadClasses {
sURL = @"www.longlist.com"
NSURL *url = [NSURL URLWithString:sURL];
sRefresh = sURL;
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[webView setDelegate:(id<UIWebViewDelegate>)self];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[webView.scrollView addSubview:refreshControl];
}
//=== Not Working
-(void)reloadWebView{
[webView reload];
}
You could scroll the webView to top by setting it's content inset to CGPointZero while loading it.
Here's the code:
What you are looking for can be acheieved with the help of Delegate Protocols.
First go through the Docs! for better understanding of the concept.
Now the code, head to your MyTabBarController.h file and add the following code.
Then on MyTabBarController.m, and update the
didSelectViewController
method like belowNow newViewController.h
Finally on newViewController.m
Cheers!