NSTokenField不让我键入其他字符串,而不是tokenField:completionsForSubstring:...返回 [英] NSTokenField does not let me type other strings than tokenField:completionsForSubstring:... returns

查看:278
本文介绍了NSTokenField不让我键入其他字符串,而不是tokenField:completionsForSubstring:...返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是NSTokenField不允许我输入任何我想要的文本,它只允许我输入包含在NSFray中的字符串tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem:returns。

$ b $ (NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex {NSInteger}(NSString *)*
return [NSArray arrayWithObjects:@AA,@BB,@CC,@DD,nil];
}

我的NSTokenField只能包含上述文本标记。
如果我输入例如XXX,它不会出现,不能添加。



为什么会出现这种情况,因为文档提到用户可以输入一个字符串



我缺少什么?

解决方案

selectedItemIndex的默认值为0 - 返回列表中的第一个项目。



因此,您需要将此项设置为 - 1,如果子串未在您的列表中显示(否则将替换用户输入您第一次完成的文本的文本)





只返回完成列表中的实际计算用户键入的前缀的内容。 (这通常是正确的用户体验。)

   - (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring: *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex 
{
NSArray * completions = [NSArray arrayWithObjects:@AA,@BB,@CC DD,nil];
NSMutableArray * filteredCompletions = [NSMutableArray array];

[completions enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL * stop){
if([[obj lowercaseString] hasPrefix:[substring lowercaseString]])
[filteredCompletions addObject:obj];
}];

return filteredCompletions;
}


My issue is that NSTokenField does not allow me to type any text I want, it's only allow me to type strings that are included in the NSArray that tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem: returns.

- (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex      {
return [NSArray arrayWithObjects:@"AA", @"BB", @"CC", @"DD", nil];
}

My NSTokenField can only contain the above text-tokens. If I type for example XXX it does not appeared and it cannot be added.

Why this happens since the documentation mentions "The user could enter a string that is not in the list of possible completions and that is also tokenized."

What am I missing ?

解决方案

The default value for selectedItemIndex is 0 — the first item in your return list.

So you either need to set this to -1 in the case that substring isn't represented in your list (otherwise it will replace the text the user typed with the text of your first completion)

or

Only return things in the completion list which actually math the prefix the user typed. (This is often the correct user experience.)

- (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex
{
   NSArray *completions = [NSArray arrayWithObjects:@"AA", @"BB", @"CC", @"DD", nil];
   NSMutableArray *filteredCompletions = [NSMutableArray array];

   [completions enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
       if ([[obj lowercaseString] hasPrefix:[substring lowercaseString]])
           [filteredCompletions addObject:obj];
   }];

   return filteredCompletions;
}

这篇关于NSTokenField不让我键入其他字符串,而不是tokenField:completionsForSubstring:...返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆