1

I add two uiwebviews in my Nib,and they load a same html in the documents dictionary.

It goes into webViewDidFinishLoad twice,but only one webview's style changed..

Thanks,here is my code:

- (void)viewDidLoad{
NSString *_pagesPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/chapter5.htm"];

web1.delegate = self;

[web1 loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:_pagesPath]]];


web2.delegate = self;
[web2 loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:_pagesPath]]];

}

- (void) webViewDidFinishLoad:(UIWebView*)webView{    
NSString *varMySheet = @"var mySheet = document.styleSheets[0];";

NSString *addCSSRule =  @"function addCSSRule(selector, newRule) {"
"if (mySheet.addRule) {"
"mySheet.addRule(selector, newRule);"                                // For Internet Explorer
"} else {"
"ruleIndex = mySheet.cssRules.length;"
"mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"   // For Firefox, Chrome, etc.
"}"
"}";

NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;line-height:3.5')"];
NSString *insertRule3 = [NSString stringWithFormat:@"addCSSRule('body', 'background:#e0d9ca;')"];

[webView stringByEvaluatingJavaScriptFromString:varMySheet];
[webView stringByEvaluatingJavaScriptFromString:addCSSRule];

[webView stringByEvaluatingJavaScriptFromString:insertRule2];
[webView stringByEvaluatingJavaScriptFromString:insertRule3];

}

Cezar
  • 55,636
  • 19
  • 86
  • 87

1 Answers1

0

I can't see anything wrong. But your two UIWebViews are the same, in this case, you should subclass UIWebView. Subclassing is a good way for reusing, makes more sense, and the most important, may solve your problem.

By the way, you should use self.web1 or self.web2 to access your properties via accessor. You should not use instance vars (web1 , web2) out side the accessor. You'd better

@synthesize web1 = _web1;

for more, read this: Question about @synthesize

Community
  • 1
  • 1
YuAo
  • 1,427
  • 1
  • 10
  • 17