自定义 NSView 更改背景颜色时崩溃
我有一个自定义 NSView 对象,带有一个名为 bgColor 的保留属性。我通过定义 setBgColor 方法来覆盖 setter 方法:
- (void)setBgColor:(NSColor *)theColor
{
[bgColor autorelease];
bgColor = [theColor retain];
[self setNeedsDisplay:YES];
}
我还有另一个名为 isOnline 的函数:
-(void)isOnline:(BOOL)connected{
if(connected){
self.bgColor = onlineBackgroundColor;
} else {
self.bgColor = offlineBackgroundColor;
}
}
当我使用 [self isOnline:NO]
在 initWithFrame 方法中调用 isOnline 方法时,它工作正常。但是,当我尝试使用以下命令从控制对象调用 isOnline 方法时:
[theCustomedView isOnline:YES];
或 theCustomedView.isOnline = YES;
它会在 setBgColor 方法中崩溃该行:bgColor = [theColor keep];
编译器抱怨程序收到信号:“EX_BAD_ACCESS”。我不明白为什么。那自动释放是错误的吗?
如果是这样,我为什么可以从控制对象 [theCustomedView setBgColor:aColor];
以及 initWithFrame 中的 self 进行调用,并且它可以正常工作?
有什么想法吗?
I have a custom NSView object with a retained property named bgColor. I override the setter method by defining setBgColor method:
- (void)setBgColor:(NSColor *)theColor
{
[bgColor autorelease];
bgColor = [theColor retain];
[self setNeedsDisplay:YES];
}
I also have another function called isOnline:
-(void)isOnline:(BOOL)connected{
if(connected){
self.bgColor = onlineBackgroundColor;
} else {
self.bgColor = offlineBackgroundColor;
}
}
When I called the isOnline method in initWithFrame method using [self isOnline:NO]
, it works fine. But when I try to call isOnline method from a controlling object with:
[theCustomedView isOnline:YES];
or theCustomedView.isOnline = YES;
It would crash in the setBgColor method at the line: bgColor = [theColor retain];
The complier complains Program received signal: "EX_BAD_ACCESS". I can't figure out why. Was that autorelease wrong?
If so, how come I can call from the controlling object [theCustomedView setBgColor:aColor];
and from self in the initWithFrame and it would work fine?
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试在
-retain
被销毁后将其发送到theColor
。检查它是从哪里来的。You're trying to send
-retain
totheColor
after it's been destroyed. Check where it's coming from.