如何处理第一响应者和NSPopover [英] How to deal with first responder and a NSPopover

查看:250
本文介绍了如何处理第一响应者和NSPopover的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图复制iTunes中搜索字段的行为,查找股票符号和名称。具体来说,当您开始在搜索字段中键入时,弹出窗口将显示已过滤的项目。在大多数情况下,我有这个工作,但我不能复制是它的方式处理第一响应



我输入三个字符后出现popover。此时,NSSearchField将失去第一个响应者状态,因此我无法继续输入。我想要的行为是
在弹出窗口出现后继续输入的能力
如果使用箭头键滚动项目,然后继续输入,您将从搜索字段中的最后一个字符继续。



我尝试的是子类化NSTextView(使用这个NSSearchField的自定义字段编辑器)和覆盖

   - (BOOL)resignFirstResponder 

在弹出窗口出现后继续键入,但显然我不能在弹出框中选择任何项目。所以我试过以下,如果向下箭头或mousedown事件发生,它返回YES。

  @interface SBCustomFieldEditor $ b {
BOOL resignFirstRepond;
}
@end

@implementation SBCustomFieldEditor

- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if(self){
//初始化代码在这里。
resignFirstRepond = NO;
}
return self;
}

- (BOOL)resignFirstResponder
{
return resignFirstRepond;
}

- (void)keyDown:(NSEvent *)theEvent
{
if([theEvent keyCode] == 125){
resignFirstRepond =是;
[self resignFirstResponder];
}
[super keyDown:theEvent];
}

- (void)mouseDown:(NSEvent *)theEvent
{
resignFirstRepond = YES;
[self resignFirstResponder];
}

这适用于mousedown事件,但不是keydown事件,

解决方案

div>

在此期间,我发现了一个容易解决。将您的文本视图子类化并实现 - (BOOL)canBecomeKeyView 。总是返回NO。当弹出窗口显示时,它只会被调用一次。您可以随时处理文本视图。


I’m trying to replicate the behaviour of the search field in iTunes, for looking up stock symbols and names. Specifically, as you start typing in the search field a popover appears with the filtered items. For the most part I have this working however what I can’t replicate is the way it handles first responder

I have my popover appear after three characters are entered. At this point the NSSearchField would lose first responder status and therefore I could no longer continue typing. The behaviour I would like is the ability to continue typing after the popover appears if scrolling through the items with the arrow keys, and then resume typing, you would continue from the last character in the Search field.

What I tried is subclassing NSTextView (use this as the custom field editor for the NSSearchField) and overriding

- (BOOL)resignFirstResponder 

By simply returning NO, I can continue typing once the popover appears, but obviously I can’t select any of the items in the popover. So i tried the following, which returns YES if the down arrow or a mousedown event occurs.

@interface SBCustomFieldEditor ()
{
    BOOL resignFirstRepond;
}
@end

@implementation SBCustomFieldEditor

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
        resignFirstRepond = NO;
    }
    return self;
}

- (BOOL)resignFirstResponder
{
    return resignFirstRepond;
}

- (void)keyDown:(NSEvent *)theEvent
{
    if ([theEvent keyCode] == 125) {
        resignFirstRepond = YES;
        [self resignFirstResponder];
    }
    [super keyDown:theEvent];
}

- (void)mouseDown:(NSEvent *)theEvent
{
    resignFirstRepond = YES;
    [self resignFirstResponder];
}

This works for the mousedown event, but not the keydown event, furthermore this doesn’t address the issue, when the user resumes typing.

Any suggestions?

解决方案

In the meantime I found an easy fix. Subclass your text view and implement - (BOOL)canBecomeKeyView. Always return NO there. It will be called only once when the popover is shown. You can work with the text view any time still.

这篇关于如何处理第一响应者和NSPopover的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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