title: NSPopover deallocation when losing focus (OSX, Objective-C)
body: <p>I am on OSX (not iOS), Xcode 8.2, ARC enabled, Objective-C.</p>
  
  <p>I have a view that opens a popover on a button click. Both are connected with a delegate and a protocol that allows access to the following methods (among others)</p>
  
  <pre><code>- (id)valueForKey:(NSString*)key;
  - (void)setValue:(id)value forKey:(NSString *)key;
  </code></pre>
  
  <p>(I'm using this protocol very often and need to keep it as clean and unspecific as possible)</p>
  
  <p>On the popover's view there's another button that opens an NSOpenPanel. Once the panel opens, the popover dissapears and get's deallocated - which is the preferred behaviour... usually. Unfortunately i need the openPanel to save to the parent view via the protocol methods. But if the popover gets deallocated those methods are not available anymore.</p>
  
  <p>So i tried to create a strong reference to the value i want to save to before the openPanel and release it afterwards to keep the popover viewController in memory. But once i try to release that... it crashes.</p>
  
  <pre><code>@interface SHWildcardItemSettingImageViewController ()
  
      @property (strong) NSString* customImagePathBookmark;
  
  @end
  
  
  @implementation SHWildcardItemSettingImageViewController
  
       // Create strong reference to delegate (to not dealloc popoverViewController)
      [self setCustomImagePathBookmark:[_delegate valueForKey:@"customImagePathBookmark"]];
  
       // Setup open panel
      NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
      [openPanel setAllowsMultipleSelection:NO];
      [openPanel setCanChooseDirectories:YES];
      [openPanel setCanChooseFiles:NO];
      [openPanel setCanCreateDirectories:YES];
      [openPanel setPrompt:@"Choose folder"];
  
      // Display the dialog box.  If the OK pressed, process the folder.
      if ( [openPanel runModal] == NSModalResponseOK ) {
  
      // Gets list of all files selected
      NSArray *files = [openPanel URLs];
  
      // Create and save Bookmark for later use (_bookmarks is a custom NSObject)
      [_delegate setValue:[_bookmarks bookmarkFromURL:[files objectAtIndex:0]] forKey:@"customImagePathBookmark"];
  }
  
      [self setCustomImagePathBookmark:nil]; // &lt;---- Crashes here
  @end
  </code></pre>
  
  <p>What would be the best way to keep the popover allocated while the NSOpenPanel is in focus WITHOUT using different NSPopoverBehaviours. </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Sometimes you don't see it... thanks to @Ron Gahlot
   - Solved with a simple bool check:</p>
  
  <pre><code>- (BOOL)popoverShouldClose:(NSPopover *)popover
  {
      // Check if imageSettings can close
      if ([popover.contentViewController isMemberOfClass:[SHWildcardItemSettingImageViewController class]])
          return ![[popover.contentViewController valueForKey:@"lockPopover"] boolValue];
  
  return YES;
  </code></pre>
  
  <p>}</p>