gpt4 book ai didi

javascript - 我正在尝试在 uiwebview 中搜索文本。我无法从 View Controller 调用函数

转载 作者:行者123 更新时间:2023-11-29 00:51:11 25 4
gpt4 key购买 nike

SearchWebView.h

#import <Foundation/Foundation.h>

@interface SearchWebView : UIWebView

- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;
- (void)removeAllHighlights;
@end

SearchWebView.m

#import "SearchWebView.h"

@implementation SearchWebView

- (NSInteger)highlightAllOccurencesOfString:(NSString*)str
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

[self stringByEvaluatingJavaScriptFromString:jsCode];

NSString *startSearch = [NSString stringWithFormat:@"window.onload = function (){uiWebview_HighlightAllOccurencesOfString('%@')}",str];
[self stringByEvaluatingJavaScriptFromString:startSearch];

NSString *result = [self stringByEvaluatingJavaScriptFromString:@"uiWebview_SearchResultCount"];
return [result integerValue];
}

- (void)removeAllHighlights
{
[self stringByEvaluatingJavaScriptFromString:@"uiWebview_RemoveAllHighlights()"];
}

@end

JavaScript 包含以下代码:

var uiWebview_SearchResultCount = 0;

function uiWebview_HighlightAllOccurencesOfStringForElement(element,keyword)
{

if (element) {
if (element.nodeType == 3) { // Text node
while (true) {
//if (counter < 1) {
var value = element.nodeValue; // Search for keyword in text node
var idx = value.toLowerCase().indexOf(keyword);

if (idx < 0) break; // not found, abort

//(value.split);

//we create a SPAN element for every parts of matched keywords
var span = document.createElement("span");
var text = document.createTextNode(value.substr(idx,keyword.length));
span.appendChild(text);

span.setAttribute("class","uiWebviewHighlight");
span.style.backgroundColor="yellow";
span.style.color="black";

uiWebview_SearchResultCount++; // update the counter

text = document.createTextNode(value.substr(idx+keyword.length));
element.deleteData(idx, value.length - idx);
var next = element.nextSibling;
element.parentNode.insertBefore(span, next);
element.parentNode.insertBefore(text, next);
element = text;
window.scrollTo(0,span.offsetTop);

}
} else if (element.nodeType == 1) { // Element node
if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') {
for (var i=element.childNodes.length-1; i>=0; i--) {
uiWebview_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
}
}
}
}
}

// the main entry point to start the search
function uiWebview_HighlightAllOccurencesOfString(keyword) {

uiWebview_RemoveAllHighlights();
uiWebview_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());
}
// helper function, recursively removes the highlights in elements and their childs
function uiWebview_RemoveAllHighlightsForElement(element) {
if (element) {
if (element.nodeType == 1) {
if (element.getAttribute("class") == "uiWebviewHighlight") {
var text = element.removeChild(element.firstChild);
element.parentNode.insertBefore(text,element);
element.parentNode.removeChild(element);
return true;
} else {
var normalize = false;
for (var i=element.childNodes.length-1; i>=0; i--) {
if (uiWebview_RemoveAllHighlightsForElement(element.childNodes[i])) {
normalize = true;
}
}
if (normalize) {
element.normalize();
}
}
}
}
return false;
}
// the main entry point to remove the highlights
function uiWebview_RemoveAllHighlights() {
uiWebview_SearchResultCount = 0;
uiWebview_RemoveAllHighlightsForElement(document.body);
}

我正在尝试像这样从我的 View Controller 调用:

-(IBAction)searchButtonPressed:(id)sender{

[_webview highlightAllOccurencesOfString:@"Bistritz"];

}

但是它显示异常`

UIWebView highlightAllOccurencesOfString:]: unrecognized selector sent to instance.

最佳答案

发生这种情况是因为您将 SearchWebView 导出连接到 UIWebView

SearchWebView 可能是 UIWebView 的子类,其中声明和定义了 highlightAllOccurencesOfString: 方法。此方法不存在于标准 UIWebView 中,这就是它抛出类似错误的原因:无法识别的选择器发送到实例

您需要在界面生成器中将 UIWebView 的类更改为 SearchWebView

  1. 去找你的身份检查员
  2. 选择您的 WebView
  3. 将 UIWebView 的类更改为 SearchWebView

Class change in identity inspector

关于javascript - 我正在尝试在 uiwebview 中搜索文本。我无法从 View Controller 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38137570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com