开发者

How to access a specific subview(UIWebView) of a UIScrollView and pass a line of Java to it

I'm creating a UIScrollView and add four UIWebViews as subviews.

I would like to do two things:

  1. Tag each WebViewwhile creating it, to address it inside the UIScrollView later.
  2. At a specific moment (scrollViewWillBeginDragging) send JavaScript to the UIWebView that is currently presented by the UIScrollview.

I don't know how to tag a subview nor how to address it. So my two questions are:

Q1. How to properly tag a subview (while adding it to a UISCrollView)?

Q2. How to address a command to a subview inside the UIScrollView? ((best case: the on that is currently shown))

Thanks for your help!

Maybe the Questions clarify by looking at the actual code:

Here is the code that I use to create the UIWebviews and add them to the 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);

Here is the code that I would like to use to Address the UIWebviews inside the UIScrollview (scrollView1):

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView1{

NSString *JavaScriptCommand = [NSString stringWithFormat:@"player.pause();"];

<strong>//HERE I'M TRYING TO ACCESS THE TAGGED VIEW</st开发者_开发技巧rong>

[scrollView1.????? stringByEvaluatingJavaScriptFromString:JavaScriptCommand];
}


This should work

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

You could store the tag of the UIWebView that is currently presented in a member of your view controller class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜