webViewDidFinishLoad在一个视图中不适用于两个uiwebviews

时间:2011-12-24 13:15:45

标签: ios uiwebview

我在笔尖中添加了两个uiwebviews,并在文档字典中加载了相同的html。

它进入webViewDidFinishLoad两次,但只有一个webview的样式发生了变化..

谢谢,这是我的代码:

- (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];

}

1 个答案:

答案 0 :(得分:0)

我看不出有什么不对。但是你的两个UIWebViews是相同的,在这种情况下,你应该是UIWebView的子类。子类化是重用,更有意义,最重要的一种好方法,可以解决您的问题。

顺便说一句,您应该使用self.web1或self.web2通过访问者访问您的属性。您不应该在访问者旁边使用实例变量(web1,web2)。你最好

@synthesize web1 = _web1;

了解更多信息,请阅读:Question about @synthesize