9

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

在我的 .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;
}

提前致谢。

4

2 回答 2

14

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

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

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

@interface PortViewController ()<UIWebViewDelegate>


@end

它应该工作。

于 2013-08-27T08:57:20.390 回答
1

delegate method should be:

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

And if you have UIWebView in xib, then probably you are trying to load request when UIWebView is nil

So make sure your myWebView instance exists.

于 2013-08-27T08:39:39.787 回答