NSTokenField代表

时间:2011-03-02 05:53:44

标签: objective-c cocoa delegates nstokenfield

Apple在“与委托的基本交互”一节中的“令牌字段编程指南”中指出,当用户在令牌字段中键入一些文本时,委托会收到tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem:消息。

当用户键入标记化字符时,委托会收到tokenField:representedObjectForEditingString:消息。

因此,当我实现这些方法时,它的行为方式就完全不同了。

我有委托方法的实现:

- (NSArray *)tokenField:(NSTokenField *)tokenFieldArg
completionsForSubstring:(NSString *)substring 
           indexOfToken:(NSInteger)tokenIndex 
    indexOfSelectedItem:(NSInteger *)selectedIndex {

NSLog(@"completionsForSubstring: %@", substring);
return [NSArray array];
}    

- (id)tokenField:(NSTokenField *)tokenField
 representedObjectForEditingString:(NSString *)editingString {

NSLog(@"representedObjectForEditingString: %@", editingString);
return editingString;
}

当我在令牌字段中键入“maksim”然后将其删除时,会有一个日志:

  

2011-03-01 19:03:01.286 Aurum [23269:a0f] representObjectForEditingString:m
  2011-03-01 19:03:01.287 Aurum [23269:a0f] representObjectForEditingString:m
  2011-03-01 19:03:01.493 Aurum [23269:a0f] completionsForSubstring:m
  2011-03-01 19:03:01.633 Aurum [23269:a0f] representObjectForEditingString:ma
  2011-03-01 19:03:01.633 Aurum [23269:a0f] representObjectForEditingString:ma
  2011-03-01 19:03:01.637 Aurum [23269:a0f] completionsForSubstring:ma
  2011-03-01 19:03:03.737 Aurum [23269:a0f] representObjectForEditingString:mak
  2011-03-01 19:03:03.737 Aurum [23269:a0f] representObjectForEditingString:mak
  2011-03-01 19:03:03.741 Aurum [23269:a0f] completionsForSubstring:mak
  2011-03-01 19:03:05.089 Aurum [23269:a0f] representObjectForEditingString:maks
  2011-03-01 19:03:05.089 Aurum [23269:a0f] representObjectForEditingString:maks
  2011-03-01 19:03:05.094 Aurum [23269:a0f] completionsForSubstring:maks
  2011-03-01 19:03:05.841 Aurum [23269:a0f] representObjectForEditingString:maksi
  2011-03-01 19:03:05.841 Aurum [23269:a0f] representObjectForEditingString:maksi
  2011-03-01 19:03:05.845 Aurum [23269:a0f] completionsForSubstring:maksi
  2011-03-01 19:03:06.697 Aurum [23269:a0f] representObjectForEditingString:maksim
  2011-03-01 19:03:06.697 Aurum [23269:a0f] representObjectForEditingString:maksim
  2011-03-01 19:03:06.701 Aurum [23269:a0f] completionsForSubstring:maksim
  2011-03-01 19:03:12.353 Aurum [23269:a0f] representObjectForEditingString:maksi
  2011-03-01 19:03:12.354 Aurum [23269:a0f] representObjectForEditingString:maksi
  2011-03-01 19:03:12.853 Aurum [23269:a0f] representObjectForEditingString:maks
  2011-03-01 19:03:12.854 Aurum [23269:a0f] representObjectForEditingString:maks
  2011-03-01 19:03:12.936 Aurum [23269:a0f] representObjectForEditingString:mak
  2011-03-01 19:03:12.937 Aurum [23269:a0f] representObjectForEditingString:mak
  2011-03-01 19:03:13.020 Aurum [23269:a0f] representObjectForEditingString:ma
  2011-03-01 19:03:13.020 Aurum [23269:a0f] representObjectForEditingString:ma
  2011-03-01 19:03:13.103 Aurum [23269:a0f] representObjectForEditingString:m
  2011-03-01 19:03:13.104 Aurum [23269:a0f] representObjectForEditingString:m

所以任何人都可以解释我出了什么问题以及为什么令牌字段表现得如此奇怪?

1 个答案:

答案 0 :(得分:1)

我只是试图通过复制和粘贴代码来重现这一点。我得到了:

2011-03-09 14:52:48.888 StackOverflowTesting[27789:a0f] completionsForSubstring: m
2011-03-09 14:52:49.242 StackOverflowTesting[27789:a0f] completionsForSubstring: ma
2011-03-09 14:52:49.560 StackOverflowTesting[27789:a0f] completionsForSubstring: mak
2011-03-09 14:52:49.911 StackOverflowTesting[27789:a0f] completionsForSubstring: maks
2011-03-09 14:52:50.017 StackOverflowTesting[27789:a0f] completionsForSubstring: maksi
2011-03-09 14:52:50.217 StackOverflowTesting[27789:a0f] completionsForSubstring: maksim
2011-03-09 14:52:50.647 StackOverflowTesting[27789:a0f] representedObjectForEditingString: maksim

这正是我期望的输出结果。您是否更改了NSTokenField的默认标记字符?当你点击标记字符时,它看起来不像是标记为maksim(因为毕竟你可以逐个字符地删除它)。