设置委托后ios webview未显示

时间:2012-12-26 19:27:29

标签: ios uiwebview delegates

我正在以编程方式设置webview,一切正常,直到我实际设置了委托。

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

以下是设置委托时页面的样子:

Page when I set the delegate

以下是我不设置委托时页面的样子:

Page when I do not set the 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>>

这是一个谜,任何帮助表示赞赏。仅供参考,我尝试评论第一个网页视图,我认为它们可能存在冲突,但它没有任何区别。

编辑:

以下是我的委托方法:

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

他们似乎都没有被召唤。

2 个答案:

答案 0 :(得分:1)

我明白了......问题出在这个方法上:

// 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语句在webview的初始加载时被捕获,因此它返回NO,这基本上给了我一个空白屏幕。在允许if语句运行之前,我必须检查这是否是初始加载。

答案 1 :(得分:0)

尝试将委托设置为属性而不是ivar。

[self.webViewForStories setDelegate:self] 

让webviews调用您已实现的委托方法。