I have an NSTabView inside my custom NSView that is used as the prototype for the NSCollectionView. In the second tab I have NSButton button and NSImageView objects.
NSButton is a "Browse" button that triggers the NSOpenPanel.
I have connected the button's selector to IBAction in MyCustomView which performs the following:
// MyView.h
@interface MyView : NSView
{
IBOutlet NSTabView *tabView;
IBOutlet NSImageView *myImageView;
IBOutlet NSButton *browseButton;
}
-(IBAction)openBrowseDialog:(id)sender;
@end
// MyView.m
-(IBAction)openBrowseDialog:(id)sender
{
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setCanChooseDirectories:NO];
[openDlg setAllowsMultipleSelection:NO];
[openDlg setAllowedFileTypes:[NSArray arrayWithObjects:@"png", @"jpg", @"jpeg", @"gif", nil]];
if ( [openDlg runModal] == NSOKButton )
{
NSArray* files = [openDlg URLs];
NSURL* fileURL = [files objectAtIndex:0];
NSData *imageData = [NSData dataWithContentsOfURL:fileURL];
if( imageData != nil )
{
NSImage *image = [[NSImage alloc] initWithData:imageData];
myImageView.image = image;
[image release];
}
}
}
When I run this "myImageView" traces "null" in the Console even though I connected it as IBOutlet in Interface Builder. Could you explain why? How should I do this instead? I also need to pass the "fileURL" value to "representedObject" in my NSCollectionViewItem object but I don't know how to access that from here?