本文介绍了允许childWindow中的视图成为关键,而不会失去对parentWindow的关注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将NSWindow的自定义子类的childWindow添加到parentWindow(也是NSWindow的自定义子类)。 childWindow具有NSBorderlessWindowMask,canBecomeKeyWindow:被重写以返回YES,canBecomeMainWindow:被返回NO。



childWindow设置为与parentWindow一起调整大小。因此,我想产生一种幻想,即childWindow的视图是parentWindow的一部分。主要思想是将基于文档的应用程序创建的文档窗口安排在一个主窗口中,以提供一个选项卡式界面(就像在浏览器中一样)以在文档之间进行切换。



我的问题是,每当我单击childWindow的视图之一时,parentWindow(主窗口)就会失去焦点,并且交通信号灯按钮会变灰。



我找到了这个答案:


I added a childWindow of a custom subclass of NSWindow to a parentWindow (also a custom subclass of NSWindow). The childWindow has the NSBorderlessWindowMask and canBecomeKeyWindow: is overridden to return YES and canBecomeMainWindow: to return NO.

The childWindow is set to resize with the parentWindow. So I want to create the illusion that the views of the childWindow are part of the parentWindow. The main idea is to arrange the document windows created by the document-based application within a main window to provide a tabbed interface (just like in a browser) to switch between the documents.

My problem is that whenever I click in one of the views of the childWindow, the parentWindow (the main window) looses focus and the traffic light buttons are getting greyed out. This is obviously contrary to what I want to achieve.

I found this answer:Make NSView in NSPanel first responder without key window status

But even if I override isKeyWindow: (of the main window) to always return YES, the title bar gets greyed out nonetheless when I click into the childWindow.

I also tried to follow this advice:http://www.cocoabuilder.com/archive/cocoa/143945-non-focused-child-window.html

But I'm not sure what "include the child window in its responder chain just ahead of its nextResponder" means. With canBecomeKeyWindow: to return NO (for the childWindow), the views within the child never can become key and are always greyed out.

Any clue what I am doing wrong?

One addition: Is it possible to make the views in the childWindow FirstResponder without giving the childWindow key-status?

解决方案

I got this working by mimicking the behaviour of NSPopover. On investigation I found that a popover (which uses a private NSPanel subclass "_NSPopoverWindow") believed it was the key & main window, even though it is not the window returned from [NSApp keyWindow].

Create your own custom NSPanel subclass, attach it to the parent window, and then override the following methods as so:

- (BOOL)isKeyWindow {
    return YES;
}

- (BOOL)isMainWindow {
    return YES;
}

- (BOOL)canBecomeKeyWindow {
    return YES;
}

- (BOOL)canBecomeMainWindow {
    return YES;
}

- (void)makeKeyWindow {
    [super makeKeyWindow];
    [self.parentWindow makeKeyWindow];
}
- (void)makeMainWindow {
    [super makeMainWindow];
    [self.parentWindow makeMainWindow];
}
- (void)becomeKeyWindow {
    [super becomeKeyWindow];
}

- (void)becomeMainWindow {
    [super becomeMainWindow];
    [self.parentWindow becomeMainWindow];
}

- (void)resignMainWindow {

}
- (void)resignKeyWindow {

}

这篇关于允许childWindow中的视图成为关键,而不会失去对parentWindow的关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:43