我已经研究和研究,但仍然不明白为什么从未调用过StartLoadWithRequest.我的页面加载正常,并调用了一些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];
把我的代表设置为自己
- (void)viewDidLoad { [super viewDidLoad]; [myWebView setopaque:NO]; [myWebView setBackgroundColor:[UIColor clearColor]]; //myWebView.description. myWebView.delegate = self; }
我的所有协议方法都被称为EXCEPT 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; }
提前致谢.
解决方法
尝试在ViewWillAppear而不是ViewDidLoad中设置委托
-(void)viewWillAppear:(BOOL)animated { myWebView.delegate = self; }
并在您的PortViewController.m文件的界面中包含webview委托
@interface PortViewController ()<UIWebViewDelegate> @end
它应该工作.