2011-12-24 30 views
1

我在我的Nib中添加了兩個uiwebviews,並且它們在文檔字典中加載了相同的html。webViewDidFinishLoad在一個視圖中不能用於兩個uiwebviews

它進入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]; 

}

回答

0

我看不到任何錯誤。但是你的兩個UIWebViews是相同的,在這種情況下,你應該繼承UIWebView。子類化是重複使用,更有意義,最重要的好方法,可以解決您的問題。

順便說一下,您應該使用self.web1或self.web2通過訪問者訪問您的屬性。你不應該在訪問者的外面使用實例變量(web1,web2)。你更多的最好

@synthesize web1 = _web1; 

,請閱讀本:Question about @synthesize

+1

謝謝,我得到了解決此:HTTP://double-slash.net/2010/10/18/using-multiple-ios -uiwebview對象/ – user1114574 2011-12-25 11:45:30

相關問題