我正在以编程方式设置一个 WebView ,并且在我实际设置委托(delegate)之前一切正常。
MainPageViewController.h
#import <UIKit/UIKit.h>
@interface MainPageViewController : UIViewController <UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webViewForStories;
@end
MainPageViewController.m
- (void)viewDidLoad
{
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 300)];
NSString *urlAddress = @"http://myurl.com/mypage";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
webViewForStories = [[UIWebView alloc] initWithFrame:CGRectMake(0, webView.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height)];
NSString *urlAddressForStories = @"http://myurl.com/mysecondpage";
NSURL *urlForStories = [NSURL URLWithString:urlAddressForStories];
NSURLRequest *requestObjForStories = [NSURLRequest requestWithURL:urlForStories];
[webViewForStories loadRequest:requestObjForStories];
[self.view addSubview:webViewForStories];
[webViewForStories setDelegate:self];
NSLog(@"webviewForStories delegate is %@", webViewForStories.delegate);
NSLog(@"webviewForStories is %@", webViewForStories);
}
这是我设置委托(delegate)时页面的样子:
这是我不设置委托(delegate)时页面的样子:
唯一的区别是这行代码:
[webViewForStories setDelegate:self];
这是 NSLog 的相关输出:
2012-12-26 14:18:40.720 WGGB for iPad[3144:907] webviewForStories delegate is <MainPageViewController: 0x1f878cc0>
2012-12-26 14:18:40.721 WGGB for iPad[3144:907] webviewForStories is <UIWebView: 0x1f8838c0; frame = (0 300; 768 1004); layer = <CALayer: 0x1f883920>>
这是一个谜,任何帮助都是值得赞赏的。仅供引用,我尝试注释掉第一个 WebView ,我认为它们可能会发生冲突,但这并没有什么区别。
编辑:
这是我的委托(delegate)方法:
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"webview finished loading");
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"webview started loading");
}
// This function allows me to intercept links clicked in a webview and open in
// a navigation window with a back button.
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(@"urlstring is %@", urlString);
// Only catching links without "mobile" in the url (i.e. stories)
if ([urlString rangeOfString:@"/mobile"].location == NSNotFound && [urlString rangeOfString:@"facebook"].location == NSNotFound && [urlString rangeOfString:@"facebook"].location == NSNotFound &&[urlString rangeOfString:@"twitter"].location == NSNotFound && [urlString rangeOfString:@"fb://"].location == NSNotFound && [urlString rangeOfString:@"liverail"].location == NSNotFound && [urlString rangeOfString:@"about:blank"].location == NSNotFound) {
//DetailViewController *detailView = [[DetailViewController alloc] init];
//[self.navigationController pushViewController:detailView animated:YES];
return NO;
} else {
return YES;
}
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"error is %@", [error localizedDescription]);
}
他们似乎都没有接到电话。
最佳答案
我明白了...问题出在这个方法上:
// This function allows me to intercept links clicked in a webview and open in
// a navigation window with a back button.
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(@"urlstring is %@", urlString);
// Only catching links without "mobile" in the url (i.e. stories)
if ([urlString rangeOfString:@"/mobile"].location == NSNotFound && [urlString rangeOfString:@"facebook"].location == NSNotFound && [urlString rangeOfString:@"facebook"].location == NSNotFound &&[urlString rangeOfString:@"twitter"].location == NSNotFound && [urlString rangeOfString:@"fb://"].location == NSNotFound && [urlString rangeOfString:@"liverail"].location == NSNotFound && [urlString rangeOfString:@"about:blank"].location == NSNotFound) {
//DetailViewController *detailView = [[DetailViewController alloc] init];
//[self.navigationController pushViewController:detailView animated:YES];
return NO;
} else {
return YES;
}
}
if 语句在 web View 的初始加载时被捕获,因此它返回 NO,这基本上给了我一个空白屏幕。在允许 if 语句运行之前,我必须检查这是否是初始加载。
关于设置委托(delegate)后,ios webview 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14045243/