Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa WebView动态插入/修改内容_Cocoa_Webview_Nsview - Fatal编程技术网

Cocoa WebView动态插入/修改内容

Cocoa WebView动态插入/修改内容,cocoa,webview,nsview,Cocoa,Webview,Nsview,在我的应用程序中,我使用WebView显示内容, 现在是否可以动态修改内容,需求是这样的 我将从网络获取信息,根据它们,我需要设置样式/字体/属性,或者可能需要在连接的设备没有响应时追加新文本 到目前为止,我使用以下代码 -(void)modifyString:(NSString *)string{ [sourceString stringByAppendingString:errorString :string] } -(void)reloadPage{ [[pWe

在我的应用程序中,我使用WebView显示内容, 现在是否可以动态修改内容,需求是这样的

我将从网络获取信息,根据它们,我需要设置样式/字体/属性,或者可能需要在连接的设备没有响应时追加新文本

到目前为止,我使用以下代码

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

   -(void)reloadPage{

     [[pWebView mainFrame] loadHTMLString:htmlString baseURL:nil];
    }
-(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];

}
我不认为这是正确的方法来实现它,我试图利用它

[pWebView replaceSelectionWithMarkupString:@"<html><body><p>Hi there </p></br></body></html>”];
[pWebView replaceSelectionWithMarkupString:@“你好


”;
但是没有显示任何内容,因为我没有选择,选择我的问题是错误的 如何设置选择

问候

罗汉

没关系 用这种方法解决它
在AwakeFromNib方法中,添加了以下代码

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

   -(void)reloadPage{

     [[pWebView mainFrame] loadHTMLString:htmlString baseURL:nil];
    }
-(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];

}
并添加此函数以附加body元素

-(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)appendTagToBody:(NSString*)标记名InnerHTML:(NSString*)InnerHTML
{
//获取所有节点的列表。
DOMNodeList*bodyNodeList=[[pWebView大型机]DOMDocument]getElementsByTagName:@“body”];
//在有效的HTML中应该只有一个,所以获取第一个domeElement。
DOMHTMLElement*bodyNode=(DOMHTMLElement*)[bodyNodeList项:0];
//使用标记名创建新元素。
DOMHTMLElement*新节点=(DOMHTMLElement*)[[pWebView大型机]DOMDocument]createElement:标记名];
//为新元素添加innerHTML。
[newNode setInnerHTML:innerHTML];
//将新元素作为最后一个子元素添加到bodyNode。
[bodyNode appendChild:newNode];
}
无论什么时候想要改变内容

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

”; [自我设置需要显示:是]; }
这似乎不会导致加载在
中添加了
标记的脚本。