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
输入三个字符后,我的弹出窗口出现。此时 NSSearchField 将失去第一响应者状态,因此我无法再继续输入。我想要的行为是
弹出窗口出现后继续输入的能力
如果使用箭头键滚动项目,然后继续输入,则将从搜索字段中的最后一个字符继续。
我尝试的是子类化 NSTextView (使用它作为 NSSearchField 的自定义字段编辑器)并覆盖
- (BOOL)resignFirstResponder
通过简单地返回“否”,我可以在弹出窗口出现后继续输入,但显然我无法选择弹出窗口中的任何项目。所以我尝试了以下操作,如果发生向下箭头或 mousedown 事件,则返回 YES。
@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];
}
这适用于 mousedown 事件,但不适用于 keydown 事件,此外,当用户恢复打字时,这并不能解决问题。
有什么建议么?
与此同时,我找到了一个简单的解决办法。子类化你的文本视图并实现- (BOOL)canBecomeKeyView
。始终返回“NO”。当弹出窗口显示时,它只会被调用一次。您可以随时使用文本视图。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)