我找到了我的答案,所以對於其他人來說,這裏是如何做到的。
首先你需要一個NSTextFinder的實例,以便你可以控制它。我們用代碼來設置它。
textFinder = [[NSTextFinder alloc] init];
[textFinder setClient:textView];
[textFinder setFindBarContainer:[textView enclosingScrollView]];
[textView setUsesFindBar:YES];
[textView setIncrementalSearchingEnabled:YES];
第一個答案:要清除視覺反饋,我可以做的兩件事情無論是。我可以取消視覺反饋...
[textFinder cancelFindIndicator];
或者,我可以提醒NSTextFinder說我要改變我的TextView的內容...
[textFinder noteClientStringWillChange];
第二個答案:有全球NSFindPboard。您可以使用它來設置搜索。
// change the NSFindPboard NSPasteboardTypeString
NSPasteboard* pBoard = [NSPasteboard pasteboardWithName:NSFindPboard];
[pBoard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeString, NSPasteboardTypeTextFinderOptions, nil] owner:nil];
[pBoard setString:@"new search" forType:NSStringPboardType];
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSTextFinderCaseInsensitiveKey, [NSNumber numberWithInteger:NSTextFinderMatchingTypeContains], NSTextFinderMatchingTypeKey, nil];
[pBoard setPropertyList:options forType:NSPasteboardTypeTextFinderOptions];
// put the new search string in the find bar
[textFinder cancelFindIndicator];
[textFinder performAction:NSTextFinderActionSetSearchString];
[textFinder performAction:NSTextFinderActionShowFindInterface]; // make sure the find bar is showing
雖然存在問題。該代碼後,查找欄中的實際文本字段不會更新。我發現,如果我切換第一響應者,那麼我可以讓它更新...
[myWindow makeFirstResponder:outlineView];
[myWindow makeFirstResponder:textView];