[英]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.