大家好,
如果我在我的 Controller View 中创建 NSTextField 那么一切都很好 - 该字段是可编辑的。 不幸的是,我必须在新的自定义 NSWindow 中创建 NSTextField。我的代码波纹管产生一个看起来没有焦点的字段(文本选择是灰色的)并且不可编辑(没有光标并且对击键没有反应)。我可以用鼠标更改文本选择,仅此而已。
我是否必须启用 NSWindow 才能接收击键?
感谢您的帮助, --约瑟夫
NSRect windowRect = [[self.window contentView] frame] ;
NSWindow* uiWindow = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered defer:YES];
[uiWindow setBackgroundColor: [NSColor redColor/*clearColor*/]];
[uiWindow setOpaque:NO];
NSView* uiView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, windowRect.size.width, windowRect.size.height)] autorelease];
[uiView translateOriginToPoint:NSMakePoint(100, uiView.bounds.size.height/2)];
uiView.wantsLayer = YES;
[uiWindow setContentView:uiView];
NSTextField *textField;
textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 800, 80)];
[textField setFont:[NSFont fontWithName:@"Helvetica Bold" size:60]];
[textField setStringValue:@"My Label"];
[textField setBezeled:YES];
[textField setDrawsBackground:YES];
[textField setEditable:YES];
[textField setSelectable:YES];
[textField setEnabled:YES];
[uiView addSubview:textField];
// THIS DOES NOT WORK
[self.window addChildWindow:uiWindow ordered:NSWindowAbove];
// THIS WORKS
//[_graphicView addSubview:uiView];
最佳答案
您需要让您的自定义窗口成为关键窗口。默认情况下,无边框窗口不能成为关键。在您的 NSWindow
子类中,添加方法 canBecomeKeyWindow:
:
- (BOOL)canBecomeKeyWindow
{
return YES;
}
你可以检查你的无边框窗口是否是关键窗口:
if([uiWindow isKeyWindow] == TRUE) {
NSLog(@"isKeyWindow!");
}
else {
NSLog(@"It's not KeyWindow!");
}
此外,对于接受按键事件的无边界窗口,该类应实现acceptFirstResponder
并返回YES
。
关于objective-c - NSTextField 在自定义 NSWindow 中不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9464423/