我的自定义 NSView 中有一个 NSTabView,用作 NSCollectionView 的原型。在第二个选项卡中,我有 NSButton 按钮和 NSImageView 对象。
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”跟踪“null”时,即使我在 Interface Builder 中将其连接为 IBOutlet。你能解释一下为什么吗?我应该怎么做呢?我还需要在我的 NSCollectionViewItem 对象中将“fileURL”值传递给“representedObject”,但我不知道如何从这里访问它?