我的自定義NSView中有一個NSTabView,用作NSCollectionView的原型。在第二個選項卡中,我有NSButton按鈕和NSImageView對象。將自定義NSView的值傳遞給NSCollectionViewItem
NSButton是一個觸發NSOpenPanel的「瀏覽」按鈕。
我在MyCustomView按鈕的選擇連接到IBAction爲執行以下操作:
// 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];
}
}
}
當我運行此「myImageView」的痕跡,儘管我連它作爲IBOutlet中在Interface Builder「零」中的控制檯。你能解釋爲什麼嗎?我應該怎麼做呢?我還需要在我的NSCollectionViewItem對象中將「fileURL」值傳遞給「representationObject」,但我不知道如何從這裏訪問它?