当“标题栏”设置为不显示时,为什么我无法在NSPanel中获得焦点?

时间:2021-06-15 21:11:53

If I create a project in Xcode 4.1, and set the MainMenu.xib to have two NSPanels, and put a NSTextField in both panels, if I set one of the NSPanels to not show the "Title Bar", then the textfield within that panel can not be clicked on or given focus.

如果我在Xcode 4.1中创建一个项目,并将MainMenu.xib设置为有两个NSPanel,并在两个面板中放置一个NSTextField,如果我将其中一个NSPanel设置为不显示“Title Bar”,那么该面板中的文本字段不能点击或给予焦点。

Why??

为什么??

1 个解决方案

#1


9  

A window (or a panel) without a titlebar cannot become key, so it can't get the focus. You have to subclass it and override its - (BOOL)canBecomeKey method, like this:

没有标题栏的窗口(或面板)不能成为键,因此无法获得焦点。你必须对它进行子类化并覆盖它的 - (BOOL)canBecomeKey方法,如下所示:

@interface MyPanel : NSPanel
@end

@implementation MyPanel

- (BOOL)canBecomeKeyWindow {
    return YES;
}

@end

#1


9  

A window (or a panel) without a titlebar cannot become key, so it can't get the focus. You have to subclass it and override its - (BOOL)canBecomeKey method, like this:

没有标题栏的窗口(或面板)不能成为键,因此无法获得焦点。你必须对它进行子类化并覆盖它的 - (BOOL)canBecomeKey方法,如下所示:

@interface MyPanel : NSPanel
@end

@implementation MyPanel

- (BOOL)canBecomeKeyWindow {
    return YES;
}

@end