2014-01-30 59 views
0

我試圖在webview組件中配置搜索欄。 我的代碼如下:searchBar操作不起作用搜索

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
     [searchBar resignFirstResponder]; 

} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    [webView highlightAllOccurencesOfString:searchBar.text]; 
    [searchBar resignFirstResponder]; 
    searchBar.text = @""; 

} 

,但它不工作,搜索這麼想的工作和鍵盤是不是hidding取消後或點擊搜索。我曾有線搜索欄

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar; 

和我seraching功能長相(我發現這個網頁上):

SearchWebView.m

@implementation UIWebView (SearchWebView) 

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

    NSString *startSearch = [NSString stringWithFormat:@"MyApp_HighlightAllOccurencesOfString('%@')",str]; 
    [self stringByEvaluatingJavaScriptFromString:startSearch]; 

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

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

@end 

SearchWebView.h @interface的UIWebView( SearchWebView)

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

@end 

SearchWebView.js

// We're using a global variable to store the number of occurrences 
var MyApp_SearchResultCount = 0; 

// helper function, recursively searches in elements and their child nodes 
function MyApp_HighlightAllOccurencesOfStringForElement(element,keyword) { 
    if (element) { 
    if (element.nodeType == 3) {  // Text node 
     while (true) { 
     var value = element.nodeValue; // Search for keyword in text node 
     var idx = value.toLowerCase().indexOf(keyword); 

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

     var span = document.createElement("span"); 
     var text = document.createTextNode(value.substr(idx,keyword.length)); 
     span.appendChild(text); 
     span.setAttribute("class","MyAppHighlight"); 
     span.style.backgroundColor="yellow"; 
     span.style.color="black"; 
     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; 
     MyApp_SearchResultCount++; // update the counter 
     } 
    } 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--) { 
      MyApp_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword); 
     } 
     } 
    } 
    } 
} 

// the main entry point to start the search 
function MyApp_HighlightAllOccurencesOfString(keyword) { 
    MyApp_RemoveAllHighlights(); 
    MyApp_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase()); 
} 

// helper function, recursively removes the highlights in elements and their childs 
function MyApp_RemoveAllHighlightsForElement(element) { 
    if (element) { 
    if (element.nodeType == 1) { 
     if (element.getAttribute("class") == "MyAppHighlight") { 
     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 (MyApp_RemoveAllHighlightsForElement(element.childNodes[i])) { 
      normalize = true; 
      } 
     } 
     if (normalize) { 
      element.normalize(); 
     } 
     } 
    } 
    } 
    return false; 
} 

// the main entry point to remove the highlights 
function MyApp_RemoveAllHighlights() { 
    MyApp_SearchResultCount = 0; 
    MyApp_RemoveAllHighlightsForElement(document.body); 
} 

回答

0

如果你的方法不燒我猜你的搜索欄委託未設置。嘗試把這個在您的viewDidLoad方法:[self.searchBar setDelegate:self];

您也可以通過點擊搜索欄,同時保持控制,並拖動到ViewController爲此在故事板。