【问题标题】:shouldStartLoadWithRequest is never calledshouldStartLoadWithRequest 永远不会被调用
【发布时间】:2013-08-29 21:59:45
【问题描述】:

我进行了研究和研究,但仍然不明白为什么从不调用 shouldStartLoadWithRequest 。我的页面加载正常,并且调用了一些 UIWebview 委托协议方法。请从我下面的代码中找到相关的 sn-ps:

在我的 .m 中合成我的 webview(在头文件中定义):

@implementation PortViewController

@synthesize WebView = myWebView;

我成功加载了我的 webview:

    myURLString = [NSString stringWithFormat:@"https://%@", defaultWebsite];

    myURL = [NSURL URLWithString: myURLString];
    NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
    [myWebView loadRequest:myRequest];

将我的委托设置为 self

- (void)viewDidLoad
{


[super viewDidLoad];
[myWebView setOpaque:NO];
[myWebView setBackgroundColor:[UIColor clearColor]];

//myWebView.description.

myWebView.delegate = self;
}

我的所有协议方法都被调用,除了 shouldStartLoadWithRequest

- (void)webViewDidStartLoad:(UIWebView *)myWebView {
    [activityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)myWebView {
    [activityIndicator stopAnimating];
}

- (BOOL)WebView:(UIWebView *)myWebView shouldStartLoadWithRequest:(NSURLRequest *)request          navigationType:(UIWebViewNavigationType)navigationType {

NSLog(@"inside Webview");
if([[request.URL absoluteString] hasPrefix:@"http://www.nzx"]) {
    // do stuff
    NSLog(@"Have caught the prefix");
    return YES;
}

    return NO;
}

提前致谢。

【问题讨论】:

  • 是错字还是您实际上实现了不正确的方法WebView:shouldStartLoadWithRequest:navigationType:W 应该是小写的webView

标签: ios methods protocols uiwebviewdelegate


【解决方案1】:

尝试在 ViewWillAppear 而不是 ViewDidLoad 中设置委托

-(void)viewWillAppear:(BOOL)animated
{
     myWebView.delegate = self;
}

并在 PortViewController.m 文件的界面中包含 webview 委托

@interface PortViewController ()<UIWebViewDelegate>


@end

它应该可以工作。

【讨论】:

  • 谢谢!!很好的答案。您可以通过不需要将接口设置为 PortViewController () 来简化它
【解决方案2】:

委托方法应该是:

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

如果您在 xib 中有 UIWebView,那么您可能正在尝试在 UIWebView 为 nil 时加载请求

所以请确保您的 myWebView 实例存在。

【讨论】:

  • 这是任性的小写 'w' 谢谢。我要去买眼镜了。
  • 这就是“自动完成”的原因。从您声明您的类支持某些协议 的那一刻起,您就可以为该委托键入具有自动完成功能的方法
猜你喜欢
  • 2012-10-27
  • 2013-10-12
  • 2012-03-27
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 2015-10-15
  • 1970-01-01
相关资源
最近更新 更多