我正在尝试将NSTokenField子类化以显示带有令牌建议的弹出窗口。
我将我的子类设置为超级委托,我拦截– tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem:
委托方法并返回nil以防止菜单显示。它工作正常,除非返回nil,令牌字段不能完成用户输入的字符串。
目前,我在– tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem:
中使用以下行添加建议的字符串:
NSText *editingText = [[self window] fieldEditor:NO forObject:self];
NSString *suggestionString = [filteredStrings objectAtIndex:0];
NSString *missingPart = [(NSString *)suggestionString substringFromIndex:[substring length]];
NSUInteger insertionPoint = [editingText selectedRange].location;
NSMutableString *currentStringPlusSuggestionString = [[editingText string] mutableCopy];
[currentStringPlusSuggestionString insertString:missingPart atIndex:insertionPoint];
[editingText setString:[stringWithSuggestionString copy]];
不幸的是,当我尝试在2个令牌之间插入文本时会产生错误。这是错误,但我真的不明白为什么字符串超出范围。
[NSBigMutableString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range or index out of bounds'
0 CoreFoundation 0x00007fff88835716 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8adae470 objc_exception_throw + 43
2 CoreFoundation 0x00007fff888354ec +[NSException raise:format:] + 204
3 Foundation 0x00007fff8efd6627 -[NSString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:] + 157
4 AppKit 0x00007fff8bd37c6f _NSFastFillAllLayoutHolesForGlyphRange + 285
5 AppKit 0x00007fff8bd008fa -[NSLayoutManager textContainerForGlyphAtIndex:effectiveRange:] + 238
6 AppKit 0x00007fff8bb4a18f -[NSTextView(NSSharing) didChangeText] + 194
7 AppKit 0x00007fff8bbe00ee -[NSTokenFieldCell textView:willChangeSelectionFromCharacterRange:toCharacterRange:] + 776
8 AppKit 0x00007fff8bd32795 -[NSControl textView:willChangeSelectionFromCharacterRange:toCharacterRange:] + 112
9 AppKit 0x00007fff8bd30724 -[NSTextView(NSSharing) setSelectedRanges:affinity:stillSelecting:] + 1076
10 AppKit 0x00007fff8bcf8438 -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 406
11 AppKit 0x00007fff8bcf828a -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 154
12 AppKit 0x00007fff8bda0f75 -[NSTextStorage processEditing] + 202
13 AppKit 0x00007fff8bc02c54 -[NSTextStorage endEditing] + 79
14 PopoverTokenField 0x0000000100002f55 -[JSTokenField tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem:] + 1013
或者,是否有人知道让令牌字段在没有菜单的情况下完成字符串的另一种方式?