Dan Hanly
Dan Hanly

Reputation: 7839

Multiple UIWebViews, how do I track webViewDidFinishLoad for both?

I've tried:

- (void) webViewDidFinishLoad:(UIWebView *)webView1{

}
- (void) webViewDidFinishLoad:(UIWebView *)webView2{

}

Errors are that I can't redefine the same method.

If I have to use the same method, I need to figure out some way of identifying one webView from the other, how would I do this?

Cheers

Upvotes: 1

Views: 1393

Answers (3)

Joe
Joe

Reputation: 57179

The reason why - (void) webViewDidFinishLoad:(UIWebView *)webView passes a webview is so that you know which webview finished loading. You have a couple of options.

  1. Create class variables for webview1 and webview2 and the compare it with webview.
  2. Tag the webviews so you know which one

1.

//SomeController.h
@interface SomeController : UIViewController
    UIWebView *webView1;
    UIWebView *webView2;
@end

//SomeController.m
...
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    if(webView == webView1) { ... }
    else if(webView == webView2) { ... }
}
...

2.

-(void)viewDidLoad
{
    webView1.tag = 1;
    webView2.tag = 2;
}

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    if(webView.tag == 1) { ... }
    else if(webView.tag == 2) { ... }
}

Upvotes: 1

Brian Donovan
Brian Donovan

Reputation: 8390

You need to keep a reference to them when you create them programmatically OR add outlets for them from Interface Builder. That way you'll have instance variables you can compare to the webView method argument to see which one has finished loading. You only need one method for this, and you may want to read up on the subject.

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   if (webView == webView1)
   {
      // handle webView1's loading
   }
   else if (webView == webView2)
   {
      // handle webView2's loading
   }
}

Upvotes: 0

mvds
mvds

Reputation: 47104

- (void) webViewDidFinishLoad:(UIWebView *)webview{
     if ( webview == self.webview1 )
     {
          // in case of webview 1
     } else if ( webview == self.webview2 ) {
          // in case of webview 2
     } else {
          NSLog(@"webview %@ was not wired to a property of %@",webview,self);
     }
}

and add webview1 and webview2 as properties to your controller. (i.e. you need the @property line and a @synthesize line)

Upvotes: 4

Related Questions