如何访问UIScrollView的特定子视图(UIWebView)并将一行Java传递给它

时间:2011-06-04 16:27:42

标签: javascript uiwebview tags uiscrollview addsubview

我正在创建UIScrollView并添加四个UIWebViews作为子视图。

我想做两件事:

  1. 在创建每个WebView时对其进行标记,以便稍后在UIScrollView内对其进行处理。
  2. 在特定时刻(scrollViewWillBeginDragging)将JavaScript发送到UIWebView当前显示的UIScrollview
  3. 我不知道如何标记子视图以及如何解决它。所以我的两个问题是:

    Q1。如何正确标记子视图(将其添加到UISCrollView中)?

    Q2。如何在UIScrollView中处理子视图的命令? ((最好的情况:当前显示的那个))

    感谢您的帮助!

    通过查看实际代码可能会澄清问题:

    以下是我用来创建UIWebviews并将其添加到UIScrollview(scrollView1)的代码:

    - (void)viewDidLoad{
    
    scrollView1.delegate = self;
    scrollView1.pagingEnabled = YES;
    scrollView1.delaysContentTouches = NO;
    
    NSInteger numberOfViews = 4;
    
    for (int i = 0; i < numberOfViews; i++) {
    
    CGFloat yOrigin = i * self.view.frame.size.width;
    UIWebView *WebView1 = [[UIWebView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"page_%d", i] ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [WebView1 loadRequest:request];
    
    
    //HERE I'M TRYING TO TAG THE VIEW
    
    WebView1.tag = i;
    
    [scrollView1 addSubview:awesomeView];
    [WebView1 release];
    
    }
    scrollView1.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
    

    以下是我想用来解决UIWebviews(scrollView1)中UIScrollview的代码:

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView1{
    
    NSString *JavaScriptCommand = [NSString stringWithFormat:@"player.pause();"];
    
    <strong>//HERE I'M TRYING TO ACCESS THE TAGGED VIEW</strong>
    
    [scrollView1.????? stringByEvaluatingJavaScriptFromString:JavaScriptCommand];
    }
    

1 个答案:

答案 0 :(得分:0)

这应该有效

int i = // the tag of the webview you want to address   
[[scrollView1 viewWithTag:i] stringByEvaluatingJavaScriptFromString:JavaScriptCommand];

您可以存储当前在视图控制器类成员中显示的UIWebView标记。