我在一个窗口中放置了一个文本字段,我只想在聚焦时才显示文本字段.我知道窗口中的所有控件都共享一个字段编辑器.我尝试了子类nstextfield并实现了becomeFirstResponder和resignFirstResponder.并尝试使用自定义单例编辑器的窗口.
谁知道如何实现这一目标?
在NSWindow中,每个文本字段或按钮共享一个字段编辑器实例(单个NSTextView实例),因此当您单击文本字段时,textfield首先成为firstResponser,然后快速将其传递给共享字段编辑器.因此,当文本字段失去焦点时,将永远不会调用文本字段的resignFirstResponder(因为字段编辑器现在是FirstResponder).
您可以在NSWindow API中查看fieldEditor:forObject :. http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow/fieldEditor:forObject:
解决方案:( 谢谢,Michael Gorbach)在我的窗口控制器中
- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
{
NSText *text = [sender fieldEditor:YES forObject:self];
if(text&&[anObject isKindOfClass:[MyCustomTextField class]])
{
[text setBackgroundColor:[NSColor whiteColor]];
[text setDrawsBackground:YES];
}
return text;
}
Run Code Online (Sandbox Code Playgroud) Lion支持单独空间中的全屏应用.通过gusture滑动空间很酷.
但我不知道如何为我自己的应用程序.