NSDrawer的生命周期对我来说没有意义。我想:
1. alloc, setParentWindow, open
2. close, setParentWindow:nil, nil out all references to really dealloc
但是,由于某种原因,setParentWindow:nil 隐藏了父窗口,如果没有,窗口中会有一个悬空引用,因此NSDrawer会泄漏。
非常简单的示例代码(NSButton附加到IBAction):
AppDelegate.h:
@interface AppDelegate : NSObject <NSApplicationDelegate>
- (IBAction)toggle:(id)sender;
@end
AppDelegate.m:
#import "AppDelegate.h"
@interface AppDelegate ()
{
NSDrawer *drawer;
}
@property (weak) IBOutlet NSWindow *window;
@end
@implementation AppDelegate
- (IBAction)toggle:(id)sender
{
if (!drawer)
{
drawer = [[NSDrawer alloc] initWithContentSize:NSMakeSize(256, 256) preferredEdge:NSMaxXEdge];
[drawer setParentWindow:_window];
[drawer open];
}
else
{
[drawer close];
[drawer setParentWindow:nil]; // incorrectly hides the window! Should only release window's reference, so drawer can be dealloc'd.
drawer = nil; // drawer is not dealloc'd here (leaks) unless setParentWindow:nil is used...
}
}
@end
这与ARC一起,见10.9.5。 setParentWindow:nil导致父窗口在抽屉滑动动画结束时隐藏,这是我不想要或理解的。该窗口仍在窗口菜单列表中,并通过那里重新显示。