Multiple UIWebViews, how do I track webViewDidFinishLoad for both?

北慕城南 提交于 2019-12-11 04:48:31

问题


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


回答1:


- (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)




回答2:


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) { ... }
}



回答3:


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


来源:https://stackoverflow.com/questions/5553610/multiple-uiwebviews-how-do-i-track-webviewdidfinishload-for-both

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!