如何验证所有令牌在NSTokenField中有效 [英] How to validate all tokens are valid in an NSTokenField

查看:201
本文介绍了如何验证所有令牌在NSTokenField中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple已经方便地创建了一个回调方法,允许您检查添加到NSTokenField的新令牌是否有效:

Apple have conveniently created a callback method that allows you to check that the new tokens that are being added to an NSTokenField are valid:

- (NSArray *)tokenField:(NSTokenField *)tokenField shouldAddObjects:(NSArray *)newTokens atIndex:(NSUInteger)index

我实现了这一点,事实证明,它的工作伟大,除了在一种情况。如果用户开始键入令牌,但尚未完成键入令牌,并且用户按TAB键,则不会调用验证方法。

I have implemented this, and it turns out that it works great except for in one case. If the user starts typing in a token, but has not yet completed typing the token, and the user presses the TAB key, the validation method is not called.

这意味着我可以确保输入的所有令牌有效,除非用户工作,他们可以按Tab键绕过验证。

This means I am able to ensure that all tokens that are entered are valid unless the user works out they can press tab to bypass the validation.

有没有人知道正确的方法处理这种情况是?

Does anyone know what the correct way to handle this situation is?

推荐答案

我试了一会儿,我发现令牌字段调用控制:isValidObject:of当按Tab键时,NSControlTextEditingDelegate协议。因此,您可以实现一个委托方法,例如

I tried for a little while and I found that the token field calls control:isValidObject: of the NSControlTextEditingDelegate protocol when the Tab key is pressed. So you can implement a delegate method such as

- (BOOL)control:(NSControl *)control isValidObject:(id)object
{
    NSLog(@"control:%@", control);
    NSLog(@"object:%@", object);
    return NO;
}

'object'参数是不完整令牌的内容。如果该方法返回NO,则不会将该令牌插入到有效令牌的数组。

The 'object' parameter is the content of your incomplete token. If the method returns NO, the token will not be inserted to the array of valid tokens.

这篇关于如何验证所有令牌在NSTokenField中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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