cocoa - NSTokenField 设置最大 Token 数量

标签 cocoa nstokenfield

假设我有一个 TokenField,用户可以在其中输入消息应发送给谁。 但我不希望用户输入超过3个Token。

有办法实现吗?

最佳答案

实现 NSTokenField 委托(delegate) tokenField:shouldAddObjects:atIndex:

// return an array of represented objects you want to add.
// If you want to reject the add, return an empty array.

- (NSArray *)tokenField:(NSTokenField *)tokenField shouldAddObjects:(NSArray *)tokens atIndex:(NSUInteger)index
{
    if (index>2) {
        return [NSArray array];
    }
    NSLog(@"%@-- %d %d", tokens, [tokens count],index);
    return tokens;
}

关于cocoa - NSTokenField 设置最大 Token 数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18272390/

相关文章:

objective-c - NSTokenField 委托(delegate)

objective-c - 单击时显示 NSTokenField token 中的 NSPopover

objective-c - 文本字段可以自动识别电话号码和链接吗?

python - 如何使用 Python 和 PyObjC 从 Apple iSight 捕获帧?

objective-c - 当 NSTokenField 成为第一响应者时,如何执行操作(显示弹出窗口)?

cocoa - NSTokenField 的 Qt 等效项

ios - 这个 MVC 模式是什么 : a stub delegate and data source

objective-c - 如何有条件地使用新的 Cocoa API

objective-c - 初始化由 nswindowcontroller 隐式调用的 nsviewcontroller

macos - 在 NSTokenField 中混合标记和字符串