Objective c 如何解除NSD抽屉的锁定?
对我来说,NSDrawer的生命周期毫无意义。我想:Objective c 如何解除NSD抽屉的锁定?,objective-c,macos,Objective C,Macos,对我来说,NSDrawer的生命周期毫无意义。我想: 1. alloc, setParentWindow, open 2. close, setParentWindow:nil, nil out all references to really dealloc 但是,setParentWindow:nil出于某种原因隐藏了父窗口,如果没有父窗口,窗口中会有一个悬空引用,因此NSDrawer会泄漏 非常简单的示例代码iAction附带了一个NSButton: AppDelegate.h: @in
1. alloc, setParentWindow, open
2. close, setParentWindow:nil, nil out all references to really dealloc
但是,setParentWindow:nil出于某种原因隐藏了父窗口,如果没有父窗口,窗口中会有一个悬空引用,因此NSDrawer会泄漏
非常简单的示例代码iAction附带了一个NSButton:
AppDelegate.h:
@interface AppDelegate : NSObject <NSApplicationDelegate>
- (IBAction)toggle:(id)sender;
@end
这是10.9.5中的ARC。setParentWindow:nil会在抽屉滑动动画完成时隐藏父窗口,我不希望也不理解这一点。该窗口仍在窗口菜单列表中,并通过该列表重新显示。假设NSDrawer实例是该窗口的所有者,因为AppDelegate不是该窗口的所有者,则当该窗口不再被抽屉引用时,该窗口将自动解除分配。在这种情况下,你可能想让抽屉一直打开,直到你不再使用窗户为止。窗户不是在这里被打开,而是被隐藏起来了。我的问题是:如何打开抽屉;我希望重复保留一个窗口和alloc/dealoc抽屉。确切地说,我的观点是,您可能必须将NSDrawer实例保留在内存中,以便窗口也保留在内存中。看看
#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