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:
[webview.scrollView setContentInset:CGPointMake(0, 0)];
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.
@protocol reloadWeb<NSObject> // The reloadWeb is our protocol and can be named as you like
-(void)reloadWebViewData; // This is the method to be called on the location you want the reload function to be called
@end
@interface MyTabBarController : UITabBarController
@property(nonatomic,assign)id<reloadWeb> reloadDelegate; // Here we create an object for the protocol.
Then on MyTabBarController.m, and update the didSelectViewController
method like below
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[newViewController class]]) { // Here newViewController is the controller where the webview reload happens.
[[[newViewController alloc] init] reloadWebViewData]; // We create and instance for the new controller and call the delegate method where the reload works.
}
}
Now newViewController.h
#import "MyTabBarController.h"
@interface newViewController : UIViewController<reloadWeb> // Here we have to confirm the protocol "reloadWeb" we created in order to access the delegate method
@end
Finally on newViewController.m
#import "newViewController.h"
#import "MyTabBarController.h"
@interface newViewController ()
@end
@implementation newViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*We have to assgin delegate to tab controller where protocol is defined so from there the "reloadWebViewData" method can be called*/
MyTabBarController * tab = [[MyTabBarController alloc] init];
tab.reloadDelegate = self;
}
-(void)reloadWebViewData{
NSLog(@"Reload Webview Data here ");
}
If you miss to add the above method ("reloadWebViewData") the Xcode will show a warning to confirm for the Protocol "reloadWeb" as the delegate method is missing.
Cheers!