ObjectiveC - WebView 不从顶部重新加载 [英] ObjectiveC - WebView not reloading from the top

查看:31
本文介绍了ObjectiveC - WebView 不从顶部重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,里面有一长串项目.我希望用户在点击标签栏项目时刷新页面.当他们点击标签栏项目时,我设法调用了加载 web 视图的方法.但它只是不会从顶部重新加载.似乎 webview 正在从它所在的位置或缓存或其他地方加载.我试图 removecahce 但它似乎不起作用.我希望它再次从顶部重新加载.我不能使用 viewWillAppear 因为我不希望页面在从 detail 页面返回时重新加载.而且我还希望在点击选项卡栏项目时重新加载页面.

在你浏览的地方重新加载,我需要从头开始重新加载.

MyTabBarController.m

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {if([viewController isKindOfClass:[ClassNavigationController 类]]) {类 *myClasses = [[类分配] 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)self];UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];[webView.scrollView addSubview:refreshControl];}//=== 不工作-(void)reloadWebView{[webView 重新加载];}

解决方案

您可以在委托协议的帮助下实现您的需求.

首先浏览文档! 以便更好地理解这个概念.

现在是代码,转到您的 MyTabBarController.h 文件并添加以下代码.

@protocol reloadWeb//reloadWeb 是我们的协议,可以随意命名-(void)reloadWebViewData;//这是要在您希望调用重载函数的位置调用的方法@结尾@interface MyTabBarController : UITabBarController@property(nonatomic,assign)id重新加载委托;//这里我们为协议创建一个对象.

然后在 MyTabBarController.m 上,更新 didSelectViewController 方法,如下所示

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{if ([viewController isKindOfClass:[newViewController class]]) {//这里 newViewController 是 webview 重新加载发生的控制器.[[[newViewController alloc] init] reloadWebViewData];//我们为新控制器创建和实例化,并调用重新加载工作的委托方法.}}

现在 newViewController.h

#import "MyTabBarController.h"@interface newViewController : UIViewController//这里我们必须确认我们创建的协议reloadWeb"才能访问委托方法@结尾

最后在 newViewController.m

#import "newViewController.h"#import "MyTabBarController.h"@interface newViewController()@结尾@implementation newViewController- (void)viewDidLoad {[超级viewDidLoad];/*我们必须将委托分配给定义了协议的选项卡控制器,以便从那里可以调用reloadWebViewData"方法*/MyTabBarController * tab = [[MyTabBarController alloc] init];tab.reloadDelegate = self;}-(void)reloadWebViewData{NSLog(@"在此处重新加载 Webview 数据");}

<块引用>

如果您错过添加上述方法(reloadWebViewData"),Xcode 将显示警告以确认协议reloadWeb",因为缺少委托方法.

干杯!

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

解决方案

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!

这篇关于ObjectiveC - WebView 不从顶部重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆