I am on OSX (not iOS), Xcode 8.2, ARC enabled, Objective-C.
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)
- (id)valueForKey:(NSString*)key;
- (void)setValue:(id)value forKey:(NSString *)key;
(I'm using this protocol very often and need to keep it as clean and unspecific as possible)
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.
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.
@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]; // <---- Crashes here
@end
What would be the best way to keep the popover allocated while the NSOpenPanel is in focus WITHOUT using different NSPopoverBehaviours.
EDIT:
Sometimes you don't see it... thanks to @Ron Gahlot - Solved with a simple bool check:
- (BOOL)popoverShouldClose:(NSPopover *)popover
{
// Check if imageSettings can close
if ([popover.contentViewController isMemberOfClass:[SHWildcardItemSettingImageViewController class]])
return ![[popover.contentViewController valueForKey:@"lockPopover"] boolValue];
return YES;
}
you use a bool variable. when openDialog is open return NO in
- (BOOL)popoverShouldClose:(NSPopover *)popover;