[英]How to resign focus on NSTextField when clicking outside
我有一个带有可编辑NSTextField
的NSView以及NSView,NSSlider,NSImage等多个其他子视图。
resignFirstResponder
,但这似乎不起作用。 我怎样才能做到这一点? 假设您有一个名为clickView1.h的NSView子类。 在参考这篇文章时 ,您可以按照以下方式在第1号中实现目标。
- (void)mouseDown:(NSEvent *)event{
AppDelegate *appDelegate = (AppDelegate *)[NSApp delegate];
[appDelegate.window makeFirstResponder:nil];
}
至于第2号,我不明白这个问题。
对于问题1,我同意BlueTomato你需要做出别的第一响应者,而不是调用resignFirstResponder
。 对于问题2,子类NSTextFieldCell
和子类中有一个如下覆盖:
- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
{
[super setUpFieldEditorAttributes: textObj];
if ([textObj isKindOfClass: [NSTextView class]])
{
NSTextView* textView = (NSTextView*) textObj;
[textView setSelectedTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSColor redColor],
NSBackgroundColorAttributeName,
nil] ];
}
return textObj;
}
请尝试以下方法:
[[NSApp mainWindow] performSelector:@selector(resignFirstResponder:)
withObject:yourTextfield
afterDelay:0.0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.