将值从自定义 NSView 传递到 NSCollectionViewItem

发布于 2024-10-15 21:24:06 字数 1354 浏览 9 评论 0原文

我的自定义 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”时,即使我在 Interface Builder 中将其连接为 IBOutlet,也会在控制台中跟踪“null”。你能解释一下为什么吗?我应该怎么做呢?我还需要将“fileURL”值传递给 NSCollectionViewItem 对象中的“representedObject”,但我不知道如何从这里访问它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浴红衣 2024-10-22 21:24:06

经过一天的排除故障,我终于实现了我所需要的目标。我原来的方法有几个问题:

1)在 NSCollectionView 中使用 NSTabView 似乎是一个坏主意,因为绑定不会在“非活动”选项卡中初始化。我放弃了这一点,并选择了 NSSegmentedControl,而不是手动显示/隐藏对象。

2)我原来问题中的所有代码都应该真正进入 NSCollectionViewItem 的子类中,而不是进入 NSView 的子类中,我什至不需要它,因为我不进行自定义绘图。

现在一切都好了。我正在学习。

I have finally achieved what I needed after a day of trouble-shooting. There were a couple of things wrong with my original approach:

1) Using NSTabView inside NSCollectionView appears to be a bad idea because bindings do not get initialized in "non-active" tabs. I scrapped that and opted for NSSegmentedControl instead with manual show/hide of objects.

2) All of the code in my original question should really go inside subclass of NSCollectionViewItem rather than into subclass of NSView, which I don't even need since I'm not doing custom drawing.

Now it's all good. I'm learning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文