事件处理程序中的 NULL 属性(cocoa 应用程序)

发布于 2024-12-19 04:55:00 字数 686 浏览 1 评论 0原文

我有一个包含几个 IBOtlet 的类:

@interface MainDreamer : NSWindow <NSWindowDelegate>
{    
    IBOutlet NSTextField *dreamField;
    IBOutlet NSTableView *dreamTable;    
    IBOutlet NSImageView *dreamview;

    IBOutlet NSMutableArray *dreamlist;  
    IBOutlet NSMutableArray *dataset;
}

一切都好,但只有在 mouseUp 事件处理程序中:

- (void) mouseUp:(NSEvent *)theEvent{
    [super mouseUp:theEvent];
    long index = [dreamTable selectedRow];
    Dream *dr = [dataset objectAtIndex:index];
    dr.dreampicture = dreamview.image;
    [dataset replaceObjectAtIndex:index withObject:dr];
}

它们全部变成 NULL。

如何访问事件处理程序内的 IBOutlet?

I have a class with several IBOtlets:

@interface MainDreamer : NSWindow <NSWindowDelegate>
{    
    IBOutlet NSTextField *dreamField;
    IBOutlet NSTableView *dreamTable;    
    IBOutlet NSImageView *dreamview;

    IBOutlet NSMutableArray *dreamlist;  
    IBOutlet NSMutableArray *dataset;
}

Everything is all right, but only in mouseUp event handler:

- (void) mouseUp:(NSEvent *)theEvent{
    [super mouseUp:theEvent];
    long index = [dreamTable selectedRow];
    Dream *dr = [dataset objectAtIndex:index];
    dr.dreampicture = dreamview.image;
    [dataset replaceObjectAtIndex:index withObject:dr];
}

all of them turn into NULL.

How to get access to IBOutlets inside event handler?

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

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

发布评论

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

评论(2

话少情深 2024-12-26 04:55:00

如何访问事件处理程序内的 IBOutlet?

你已经拥有了。出口只是实例变量(或属性,如果您这样声明的话);插座或响应器方法没有什么特别之处,会导致其中一种方法无法在另一种方法中工作。

那么,为什么您的出口在代码的某些部分显示为已连接,而在其他部分则保持 nil 呢?

通常,这是因为您在笔尖中创建了一个 MainDreamer 对象,并且在代码中创建了一个 MainDreamer 对象,或者您在两个单独的笔尖中创建了该对象。

无论哪种方式,这都会使两个对象属于同一类。它们不是同一个对象,其中一个的插座已连接,而另一个则没有。您单击的不是您在笔尖(或其中一个笔尖)中创建并连接的笔尖。

证明这一点的方法是在 mouseUp: 方法中以及在您发现要连接的插座的任何位置记录 self。我希望您会发现 self 的不同值:正如我所说,两个对象。

解决方案是删除其中一个实例并将其所有用途更改为使用另一个实例。如果您在代码中创建对象之一,那么您应该删除该对象,以支持笔尖中的对象。

How to get access to IBOutlets inside event handler?

You already have it. Outlets are just instance variables (or properties, if you declare them as such); there is nothing special about outlets or responder methods that causes one to not work in the other.

So then why do your outlets appear connected in some parts of your code and hold nil in others?

Usually, this is because you have created a MainDreamer object in the nib and created one in code, or you have created it in two separate nibs.

Either way, that makes two objects of the same class. They are not the same object, and one has its outlets connected and the other doesn't. The one you are clicking on is not the one you created and hooked up in the nib (or one of the nibs).

The way to prove this would be to log self both in the mouseUp: method and in any place where you've found the outlets to be connected. I expect you will find different values of self: as I said, two objects.

The solution is to remove one of the instances and change all uses of it to use the other instead. If you are creating one of the objects in code, that is the one you should remove, in favor of the one in the nib.

海之角 2024-12-26 04:55:00

如果 mouseUp 是 MainDreamer 对象中的方法,则这些属性应该可用。您确定已正确连接 Interface Builder 中的所有插座吗?尝试将其添加到您的对象中:

- (void)awakeFromNib {
    NSLog("table: %@", dreamTable);
    NSLog("view: %@", dreamview);
    NSLog("dataset: %@", dataset);
}

如果它们为null,则说明它们连接不正确。

If mouseUp is a method in your MainDreamer object, the properties should be available. Are you sure you've correctly connected all the outlets in Interface Builder? Try adding this to your object:

- (void)awakeFromNib {
    NSLog("table: %@", dreamTable);
    NSLog("view: %@", dreamview);
    NSLog("dataset: %@", dataset);
}

If they are null you don't have them connected properly.

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