绕过 IBActions 有限范围

发布于 2024-09-01 15:15:12 字数 1110 浏览 8 评论 0原文

我有一个 NSCollectionView ,视图是一个带有标签和 NSButton 的 NSBox 。我想要双击或单击 NSButton 来告诉控制器使用 NSCollectionViewItem 表示的对象执行操作。 Item View 已经被子类化,代码如下:

#import <Cocoa/Cocoa.h>
#import "WizardItem.h"

@interface WizardItemView : NSBox {
    id delegate;
    IBOutlet NSCollectionViewItem * viewItem;
    WizardItem * wizardItem;
}

@property(readwrite,retain) WizardItem * wizardItem;
@property(readwrite,retain) id delegate;

-(IBAction)start:(id)sender;

@end

#import "WizardItemView.h"

@implementation WizardItemView
@synthesize wizardItem, delegate;

-(void)awakeFromNib {   
    [self bind:@"wizardItem" toObject:viewItem withKeyPath:@"representedObject" options:nil];
}

-(void)mouseDown:(NSEvent *)event {
    [super mouseDown:event];
    if([event clickCount] > 1) {
        [delegate performAction:[wizardItem action]];
    }   
}

-(IBAction)start:(id)sender {
    [delegate performAction:[wizardItem action]];
}


@end

我遇到的问题是,作为 IBAction,-start 范围内唯一的东西就是 IB 中绑定的东西,所以 delegate 和 viewItem 。这意味着我无法获取所表示的对象并将其发送给委托。

有没有办法绕过这个有限的范围或更好的方法或获取所表示的对象?

谢谢。

I have an NSCollectionView and the view is an NSBox with a label and an NSButton. I want a double click or a click of the NSButton to tell the controller to perform an action with the represented object of the NSCollectionViewItem. The Item View is has been subclassed, the code is as follows:

#import <Cocoa/Cocoa.h>
#import "WizardItem.h"

@interface WizardItemView : NSBox {
    id delegate;
    IBOutlet NSCollectionViewItem * viewItem;
    WizardItem * wizardItem;
}

@property(readwrite,retain) WizardItem * wizardItem;
@property(readwrite,retain) id delegate;

-(IBAction)start:(id)sender;

@end

#import "WizardItemView.h"

@implementation WizardItemView
@synthesize wizardItem, delegate;

-(void)awakeFromNib {   
    [self bind:@"wizardItem" toObject:viewItem withKeyPath:@"representedObject" options:nil];
}

-(void)mouseDown:(NSEvent *)event {
    [super mouseDown:event];
    if([event clickCount] > 1) {
        [delegate performAction:[wizardItem action]];
    }   
}

-(IBAction)start:(id)sender {
    [delegate performAction:[wizardItem action]];
}


@end

The problem I've run into is that as an IBAction, the only things in the scope of -start are the things that have been bound in IB, so delegate and viewItem. This means that I cannot get at the represented object to send it to the delegate.

Is there a way around this limited scope or a better way or getting hold of the represented object?

Thanks.

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

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

发布评论

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

评论(1

一场春暖 2024-09-08 15:15:12

首先,您几乎不需要对视图进行子类化。

Bind 不会按照您的想法进行操作 - 您想要 addObserver:forKeyPath:options:context: (您应该尝试了解 -bind 的含义)。

当您说“关键似乎是 NSCollectionViewItem 的“原型”视图”时,我认为您真的很困惑……

忘记 IBOutlet 和 IBOutlet 吧。 IBAction - 如果您不是 Interface Builder,那么它们没有任何意义。 “原型”在 Objective-c 中没有任何意义。

视图中的两个方法没有任何不同的作用域——它们之间根本没有区别。它们都是方法,除了它们的名称(当然还有它们包含的代码)之外,在所有方面都是等效的。

如果 WizardItem 在 -start 中为 null,但在 -mouseDown 中具有值,则这完全与调用它们的时间有关。您要么拥有一个即将消失的对象,要么在您认为它应该创建的时候尚未创建。

你熟悉 NSZombie 吗?你会发现它非常有用。

Firstly, you almost never need to subclass views.

Bind doesn't do what you think - you want addObserver:forKeyPath:options:context: (You should try to understand what -bind is for tho ).

When you say "the key seems to be it being the "prototype" view for an NSCollectionViewItem" I think you are really confused…

Forget IBOutlet & IBAction - they don't mean anything if you are not Interface Builder. "Prototype" means nothing in Objective-c.

The two methods in the view do not have different scope in any way - there is no difference between them at all. They are both methods, equivalent in every way apart from their names (and of course the code they contain).

If wizardItem is null in -start but has a value in -mouseDown this is wholly to do with the timing that they are called. You either have an object that is going away too soon or isn't yet created at a point you think it is.

Are you familiar with NSZombie? You will find it very useful.

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