2011-03-15 93 views
2

在我的應用程序,我使用的WebView來顯示內容, 現在是可以動態修改的內容,要求是這樣的,的WebView插入/修改內容動態

我會得到網絡信息並且根據他們我需要設置樣式/字體/屬性或可能我需要追加當連接的設備沒有響應新的文本,

到目前爲止我用下面的代碼,

-(void)modifyString:(NSString *)string{ 
    [sourceString stringByAppendingString:errorString :string] 
} 

    -(void)reloadPage{ 

    [[pWebView mainFrame] loadHTMLString:htmlString baseURL:nil]; 
    } 

我不'牛逼認爲它正確的方式來實現它,我試圖利用

[pWebView replaceSelectionWithMarkupString:@"<html><body><p>Hi there </p></br></body></html>」]; 

,而是因爲,我沒有選擇,並選擇我的問題是 如何設置選擇什麼也不顯示?

親切的問候

羅漢

回答

10

沒關係 通過這種方式
在AwakeFromNib方法解決了這個問題,添加以下代碼,

-(void)awakeFromNib{ 

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"about:blank"]]; 

    //URL Requst Object 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    //Load the request in the UIWebView. 
    [[pWebView mainFrame ]loadRequest:requestObj]; 
    [pWebView setEditable:YES]; 
    [pWebView setNeedsDisplay:YES]; 

} 

並添加該功能到主體元件附加,

-(void)appendTagToBody:(NSString *)tagName InnerHTML:(NSString *)innerHTML 
{ 
    // Gets a list of all <body></body> nodes. 
    DOMNodeList *bodyNodeList = [[[pWebView mainFrame] DOMDocument] getElementsByTagName:@"body"]; 

    // There should be just one in valid HTML, so get the first DOMElement. 
    DOMHTMLElement *bodyNode = (DOMHTMLElement *) [bodyNodeList item:0]; 

    // Create a new element, with a tag name. 
    DOMHTMLElement *newNode = (DOMHTMLElement *) [[[pWebView mainFrame] DOMDocument] createElement:tagName]; 

    // Add the innerHTML for the new element. 
    [newNode setInnerHTML:innerHTML]; 

    // Add the new element to the bodyNode as the last child. 
    [bodyNode appendChild:newNode]; 
} 

每當想改變的內容,

-(void)appendString:(NSString *)pString{ 
    [self appendTagToBody:@"div" InnerHTML:@"<div><p> Hi there </p></div>"]; 
    [self setNeedsDisplay:YES]; 
} 
+1

這似乎並沒有導致與''加載''