Dan Hanly
Dan Hanly

Reputation: 7839

Setting the Delegate for Multiple UIWebViews on the same ViewController

I have two UIWebViews, one to handle normal files and one to handle PDFs (which opens as an overlay, on top of the other web view).

Both webviews are setup as below:

//Options for webView
webView.delegate = self;
webView.scalesPageToFit = YES;
for (id subview in webView.subviews)
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        ((UIScrollView *)subview).bounces = NO;

//Options for pdfViewer
pdfViewer.delegate = self;
pdfViewer.scalesPageToFit = YES;
for (id subview in pdfViewer.subviews)
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        ((UIScrollView *)subview).bounces = NO;

the line pdfViewer.delegate = self; causes my app some trouble. I have a delegate method I'm making use of webViewDidFinishLoad and so I need the delegate of both views to be self. If it's not set, i.e. I comment it out. The PDF loads in the view, but the webViewDidFinishLoad doesn't fire, as you'd expect. However, if I re-activate the line, the PDF doesn't load into the view at all. Is there some sort of clash happening? Can I assign multiple UIWebViews delegate = self?

Update:

#import "MyViewController.h"

@implementation MyViewController

@synthesize webView;

//these two just appear to show the page is loading.
@synthesize loadingView;
@synthesize linkLoadView;

//pdfViewer is contained within topView
@synthesize topView;
@synthesize pdfViewer;


- (void)viewDidLoad {
[super viewDidLoad];    

linkLoadView.alpha=0.0;
[topView removeFromSuperview];

//Options for 
webView.delegate = self;
webView.scalesPageToFit = YES;
for (id subview in webView.subviews)
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        ((UIScrollView *)subview).bounces = NO;

//Options for 
//pdfViewer.delegate = self;
pdfViewer.scalesPageToFit = YES;
for (id subview in pdfViewer.subviews)
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        ((UIScrollView *)subview).bounces = NO;

NSURL *filePath = [NSURL URLWithString:@"*webaddress*/index.html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:filePath];
[webView loadRequest:requestObj];
}

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;  {
    /* Add Content Loading Banner */
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
       [self.view addSubview:linkLoadView];
       linkLoadView.alpha = 1.0;
    }

    /* Handle PDF Opening */
    NSURL *url = [request URL];
    NSString *urlString = [url absoluteString];
    if([urlString rangeOfString:@".pdf"].location == NSNotFound){
        return true;
    } else {
        NSURL *filePath = [NSURL URLWithString:urlString];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:filePath];
        [pdfViewer loadRequest:requestObj];

        [self.view addSubview:topView];
        [self.view addSubview:linkLoadView];

        return false;
    }

}

- (void) webViewDidFinishLoad:(UIWebView *)theWebView{
    NSLog(@"Fired");    
}

@end

Upvotes: 0

Views: 3068

Answers (1)

Simon Lee
Simon Lee

Reputation: 22334

Are you overriding any other methods from the UIWebView delegate like shouldLoadRequest etc....?

There is no issue with having a single delegate to multiple webviews so I am guessing that something is stopping the normal request process somewhere.

Upvotes: 1

Related Questions