0

我正在以编程方式设置 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);
}

这是我设置委托时页面的样子:

我设置委托时的页面

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

未设置委托时的页面 唯一的区别是这行代码:

[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,我认为它们可能会发生冲突,但这并没有什么不同。

编辑:

这是我的委托方法:

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

他们似乎都没有接到电话。

4

2 回答 2

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 语句运行之前,我必须检查这是否是初始加载。

于 2012-12-27T14:18:40.830 回答
0

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

[self.webViewForStories setDelegate:self] 

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

于 2012-12-26T20:23:13.060 回答