首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当NSTokenField成为第一个响应者时,我如何执行操作(显示弹出)?

当NSTokenField成为第一个响应者时,我如何执行操作(显示弹出)?
EN

Stack Overflow用户
提问于 2012-09-02 05:19:28
回答 1查看 571关注 0票数 1

我正在尝试子类NSTokenField,以便在控件获得焦点后显示带有一些选项的NSPopover或菜单。不幸的是,经过几天的试验,我开始认为这是不可能的。

以下是我到目前为止尝试过的:

  • -textDidBeginEditing-controlTextDidBeginEditing是无用的,因为它们只在用户输入第一个字母之后才被调用。我需要一些能让我专注的东西。
  • 覆盖-becomeFirstResponder-resignFirstResponder以显示和隐藏菜单或弹出也是没用的。令牌字段显然将第一个响应状态传递给一个私有视图(NSTokenFieldView),因此在-becomeFirstReponder关闭菜单或弹出后立即调用-resignFirsttResponder
  • 在删除-currentEditor中的菜单之前,我试着查看-resignFirstResponder的值。当控件处于编辑模式时,-currentEditor应该是非零的,但不幸的是,它的值只在调用令牌字段中的-resignFirstResponder之后才设置,并且再次立即关闭菜单。
  • 我尝试对NSTokenFieldCell进行子类化,并覆盖它的-editWithFrame:-selectWithFrame:方法,但是没有显示带有自定义NSTokenFieldCell的令牌字段,也没有报告错误或异常。

知道怎么做吗?有人做过吗?

EN

回答 1

Stack Overflow用户

发布于 2012-09-02 07:06:47

这个很管用。我通过观察父窗口的firstResponder属性来做到这一点。如果令牌字段或其包含的任何NSResponder__s成为第一个响应器,则显示弹出窗口。

代码语言:javascript
复制
@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    [ self.window addObserver:self forKeyPath:@"firstResponder" options:NSKeyValueObservingOptionNew context:nil ] ;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ( object == self.window && [ keyPath isEqualToString:@"firstResponder" ] )
    {
        NSResponder * responder = [ change valueForKey:NSKeyValueChangeNewKey ] ;

        while ( NULL != responder )
        {
            if ( responder == self.tokenField )
            {
                // show popover (if not showing)
                NSLog(@"Show popover!\n") ;
                return ;
            }
            responder = responder.nextResponder ;
        }

        NSLog(@"Hide popover!\n") ;
    }
    else
    {
        [ super observeValueForKeyPath:keyPath ofObject:object change:change context:context ] ;
    }
}

@end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12233614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档