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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 NSTextView+;NSTextFinder+;我的独立搜索字段_Cocoa_Search_Nstextview_Nssearchfield - Fatal编程技术网

Cocoa NSTextView+;NSTextFinder+;我的独立搜索字段

Cocoa NSTextView+;NSTextFinder+;我的独立搜索字段,cocoa,search,nstextview,nssearchfield,Cocoa,Search,Nstextview,Nssearchfield,我正在尝试使用来自自定义NSSearchField的搜索查询在NSTextView中实现搜索 听起来很简单,但我不能让它工作 到目前为止,我已经浏览了所有关于NSTextFinder、其客户端和FindBarContainer的苹果文档。TextFinder只是向容器提供FindBarView,当您激活搜索时,容器会显示它 客户端、容器和TextFinder之间的所有通信都是隐藏的。它看起来就像一个黑匣子,设计成“按原样”工作,没有任何定制或干扰 但是关于NSTextFinder的-(void)

我正在尝试使用来自自定义NSSearchField的搜索查询在NSTextView中实现搜索

听起来很简单,但我不能让它工作

到目前为止,我已经浏览了所有关于
NSTextFinder
、其客户端和FindBarContainer的苹果文档。TextFinder只是向容器提供FindBarView,当您激活搜索时,容器会显示它

客户端、容器和TextFinder之间的所有通信都是隐藏的。它看起来就像一个黑匣子,设计成“按原样”工作,没有任何定制或干扰

但是关于NSTextFinder的
-(void)performation:(NSTextFinderAction)op
方法呢?它不是用来向TextFinder发送自定义命令的吗

我试图用以下内容为其分配新的搜索字符串:

    NSPasteboard* pBoard = [NSPasteboard pasteboardWithName:NSFindPboard];
    [pBoard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeString, NSPasteboardTypeTextFinderOptions, nil] owner:nil];
    [pBoard setString:_theView.searchField.stringValue forType:NSStringPboardType];
    NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithBool:YES], NSTextFinderCaseInsensitiveKey,
                              [NSNumber numberWithInteger:NSTextFinderMatchingTypeContains], NSTextFinderMatchingTypeKey,
                              nil];

    [pBoard setPropertyList:options forType:NSPasteboardTypeTextFinderOptions];

    [textFinder performAction:NSTextFinderActionSetSearchString];
但这不起作用,只会破坏正常的findBar操作

我有一种强烈的感觉,我做错了什么。 我只想在自己的NSSearchField中有一个标准的搜索功能。可能吗

我打赌我不是第一个对普通的findBar不满意的人


非常需要你的帮助,非常感谢

您可以使用
NSComboBox
。使用以下委托返回搜索值:

- (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)substring
{    
    if ([aComboBox tag] == 101 || [aComboBox tag] == 102) {
        NSArray *currentList;
        if ([aComboBox tag] == 101) {
            NSArray *keyArray = keySuggestions;
            currentList = keyArray;
        } else {
            currentList = [NSArray arrayWithArray:self.valueSuggestions];
        }
        NSEnumerator *theEnum = [currentList objectEnumerator];
        id eachString;
        NSInteger maxLength = 0;
        NSString *bestMatch = @"";
        while (nil != (eachString = [theEnum nextObject])) {
            NSString *commonPrefix = [eachString
                                      commonPrefixWithString:substring options:NSCaseInsensitiveSearch];
            if ([commonPrefix length] >= [substring length] && [commonPrefix
                                                                length] > maxLength)
            {
                maxLength = [commonPrefix length];
                bestMatch = eachString;
                break;
            }
        }
        return bestMatch;
    }

    return substring;
}

谢谢但这并不能回答我的问题。问题不在搜索领域。我想实现一种使用自定义搜索字段搜索TextView的简单方法。使用NSTextFinder会很好。但如果不可能,请提供其他一些工作解决方案。请注意,创建自定义搜索字段不是问题。我一直在为NSTextView实现搜索机制,这将允许使用我的自定义搜索字段。你知道这一点吗?没有。最后使用了默认的查找栏。但很快就会回到这里,你可能会发现这很有用。