不幸的是,有沒有乾淨的方式來做到這一點。幸運的是,雖然,我已經做到了美味的圖書館醜陋的方式3,您需要把這個方法NSWindow的子類,並確保有問題的文檔窗口是子類:
- (BOOL)makeFirstResponder:(NSResponder *)responder;
{
// Prevent popover content view from forcing our current first responder to resign
if (responder != self.firstResponder && [responder isKindOfClass:[NSView class]]) {
NSWindow *const newFirstResponderWindow = ((NSView *)responder).window;
NSWindow *currentFirstResponderWindow;
NSResponder *const currentFirstResponder = self.firstResponder;
if ([currentFirstResponder isKindOfClass:[NSWindow class]])
currentFirstResponderWindow = (id)currentFirstResponder;
else if ([currentFirstResponder isKindOfClass:[NSView class]])
currentFirstResponderWindow = ((NSView *)currentFirstResponder).window;
// Prevent some view in popover from stealing our first responder, but allow the user to explicitly activate it with a click on the popover.
// Note that the current first responder may be in a child window, if it's a control in the "thick titlebar" area and we're currently full-screen.
if (newFirstResponderWindow != self && newFirstResponderWindow != currentFirstResponderWindow && self.currentEvent.window != newFirstResponderWindow)
for (NSView *responderView = (id)responder; responderView; responderView = responderView.superview)
if ([responderView conformsToProtocol:@protocol(LIPopoverFirstResponderStealingSuppression)] &&
((id <LIPopoverFirstResponderStealingSuppression>)responderView).suppressFirstResponderWhenPopoverShows)
return NO;
}
return [super makeFirstResponder:responder];
}
現在做確保酥料餅的內容視圖子類實現此協議:
// NSPopover doesn't respect -acceptsFirstResponder of its content view (Radar 10666891).
@protocol LIPopoverFirstResponderStealingSuppression <NSObject>
@property (readonly, nonatomic) BOOL suppressFirstResponderWhenPopoverShows;
@end
也請把與蘋果的錯誤,請求NSPopover尊重其內容視圖的-acceptsFirstResponder;當多個開發者提交bug修復時,這是100%的情況。
據推測,加載到「NSPopover」中的「視圖」是從NIB文件配置並通過視圖控制器加載的?該NIB文件中的* first responder *設置是否正確? AFAIK,popover應該遵守窗口和視圖的通常的* first responder *行爲。 – trojanfoe
@tojanfoe對不起,這個問題可能被誤解了。 NSTokenField不在popover內。爲了清晰起見,我編輯了該問題並添加了一張圖片。 – Lukas
也許你需要使用「NSPopover」以外的東西。 popover的預期行爲是提供一些用戶可以與之交互的控件。 – trojanfoe