Objective-C: Popover seems to be always nil

68 views Asked by At

I'm currently writing a plugin for SketchApp with cocoa.

I try to use a NSPopover there, that should get triggered by an IBAction when clicking on a button. Problem is: the popover does not show up, and when inspecting the variable, that should hold the popover, it is nil.

I created the NSPopover in the Interface Builder, so not programmatically in code; then defined an IBOutlet binding in my linked classes header file; and finally use this variable inside my implementation class.

Here is my source code:

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];
}

In my xib-view-file, I linked the NSPopover-Object to the IBOutlet NSPopover *popover;. But when inspecting _popover in my class implementation it is always nil.

1

There are 1 answers

3
jlang On BEST ANSWER

Okay, after tweeking things around a bit, I guess I finally understood what went wrong. After changing the memory mode from @property (nonatomic, weak) NSPopover *popover; to ... (nonatomic, strong) ...; the instance is not nil anymore.

I think the difference to the NSButton (for example) here is, that the button is used in the xib template, so the button's reference does not get cleaned up. The Popover however is not directly "used", so it may gets cleaned up, before it could get actually used. So, changing it from weak to strong seems to be the point here.

However: Does this tweak have unexpected / unintended side effects I probably want to address somehow? Do I have to care for the destruction on my own for the popover now?