我正在创建一个使用 NSTokenField 的应用程序。我需要自动完成。我正在使用 NSURLRequest 从 http://stackoverflow.com/filter/tags?_=<timestamp>&limit=6&q=<str_to_autocomplete>×tamp=<timestamp>
请求数据
哪里<timestamp>
是当前时间戳,<str_to_autocomplete>
是要自动完成的字符串。所以,例如http://stackoverflow.com/filter/tags?_=1263657227137&q=lol&limit=6×tamp=1263657227137
响应的格式如下:
javascript|23179
jquery|16936
sql-server|11768
ruby-on-rails|8669
best-practices|7037
ruby|6722
(数字是该标签的使用次数)。
我需要在 NSTokenField 下向用户提供一个包含此标签列表的列表,用户可以选择其中一个列表,也可以继续输入。
有人可以帮助我吗?谢谢。
编辑:我现在正在查看 Mac 开发中心。我应该使用这种方法吗:tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem:
?
最佳答案
这将发送给委托(delegate)来查询字符串数组:
tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem:
这些字符串应该由 representedObject
中的 tokenField
处理(如果您只需要字符串,则不处理)。
tokenField
委托(delegate)中的示例:
- (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex
{
//code to find the tags strings corresponding to substring (the string typed in the token)
//then put them in an array (returnArray)
return returnArray;
}
tokenField
将在您键入时在菜单中显示完成的字符串。所有详细信息都在文档中。
关于objective-c - NSTokenField 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2077914/