I am running into a problem with the NSOpenPanel and calling it form another class.
I currently have one main window with a button that opens up a second window that is setup as an image editor using ImageKit. That works well. I would also like for when that image editor window opens up (as a result of the button push) the NSOpenPanel is launched. Basically I want to bypass making the user click a button to open the image editor and then click "Open" in the menu or command-O to open an image. We know that if the user is opening the image editor they will need to open an image to edit... I'd like the open panel to open when the window is displayed.
In my appDelegate.m I have this code to launch the image editor window "_imageWindow" and call the "openImage" method:
[_imageWindow makeKeyAndOrderFront:self];
Controller *controllerOpenImage = [[Controller alloc] init];
[controllerOpenImage openImage];
This works EXCEPT that the open panel which is supposed to be modal is launched as a separate window and not attached to the Image Editor window (_imageWindow) so when a user selects an image it's not opened... I've tried adding a delay to allow the _imageWindow window time to open to no avail. I've tried both IBAction(openImage) and void(openImage) with and without sender with the same result...
Here's the code to open an image in my Controller.m:
- (IBAction)openImage:(id)sender
{
// present open panel...
NSString * extensions = @"tiff/tif/TIFF/TIF/jpg/jpeg/JPG/JPEG";
NSArray * types = [extensions pathComponents];
NSString *url=[[NSUserDefaults standardUserDefaults] objectForKey:@"photoPath"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
NSString *yearString = [formatter stringFromDate:[NSDate date]];
NSString *photoUrl =[NSString stringWithFormat:@"%@%@%@",url,@"/",yearString];
// Let the user choose an output file, then start the process of writing samples
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setDirectoryURL:[NSURL fileURLWithPath:photoUrl]];
[openPanel setAllowedFileTypes:types];
[openPanel setCanSelectHiddenExtension:YES];
[openPanel beginSheetModalForWindow:_imageWindow completionHandler:^(NSInteger result) {
if (result)
{
// user did select an image...
[self openImageURL: [openPanel URL]];
}
[_imageView zoomImageToFit: self];
}];
}
Is there an issue with the sender being null when called from my appDelegate.m as opposed to sender having an identity when called from the Image Editor window (_imageEditor) or am I asking to do something that just can't be done.