Objective-C: Popover 似乎总是 nil

Objective-C: Popover seems to be always nil

我目前正在使用 cocoa 为 SketchApp 编写插件。

我尝试在那里使用 NSPopover,点击按钮时应该会被 IBAction 触发。问题是:popover 没有出现,并且在检查变量时,它应该包含 popover,它是 nil.

我在 Interface Builder 中创建了 NSPopover,所以不是在代码中以编程方式创建的;然后在我链接的 classes 头文件中定义了一个 IBOutlet 绑定;最后在我的实现中使用这个变量 class.

这是我的源代码:

MyComponent.h

// imports skipped...

@interface

@property (nonatomic, weak) IBOutlet NSTextField *componentDescription;
@property (nonatomic, weak) IBOutlet NSTextField *componentGuid;
@property (nonatomic, weak) IBOutlet NSButton *guidCopyButton;
@property (nonatomic, weak) IBOutlet NSPopover *popover;

-(IBAction)onCopyButton_Clicked:(id)sender;

@end

MyComponent.m

-(IBAction)onCopyButton_Clicked:(id)sender {

    // copy stuff to clipboard
    // [...]

    // show copied popover
    [_popover showRelativeToRect:[sender bounds] 
                          ofView:sender 
                   preferredEdge:NSMinYEdge];
}

在我的 xib-view-file 中,我将 NSPopover-Object 链接到 IBOutlet NSPopover *popover;。但是在我的 class 实现中检查 _popover 时,它总是 nil.

好吧,经过一番调整,我想我终于明白哪里出了问题。将内存模式从 @property (nonatomic, weak) NSPopover *popover; 更改为 ... (nonatomic, strong) ...; 后,实例不再是 nil

我认为这里与 NSButton(例如)的不同之处在于,按钮在 xib 模板中 使用,因此按钮的引用没有得到清理干净。然而,Popover 并不是直接 "used",因此它可能会在实际使用之前被清理干净。因此,将它从 weak 更改为 strong 似乎是这里的重点。

但是: 这个调整是否有我可能想以某种方式解决的意想不到/意外的副作用?我现在必须自己处理弹出窗口的破坏吗?