无法在 ObjC 中设置对象?
每当我的方法将一个对象设置为 onTouchEventReceiver 时,当另一个线程调用另一个方法时,该对象就会丢失。
//In header
id onTouchEventReceiver;
SEL onTouchSelector;
- (void) setOnTouchSelector:(SEL)sel withObject:(id)obj
{
NSLog(@"setting obj to %@",obj);
onTouchSelector = sel;
[self setOnTouchEventReceiver:obj];
NSLog(@"====----- %@",onTouchEventReceiver); //That works
}
//Another thread calls this
- (void) touchEventReceived
{
NSLog(@"firing a selector at %@ by %@",onTouchEventReceiver,self);
//Why on earth does that happen?????
if (onTouchEventReceiver != nil) //onTouchEventReceiver is (null)
{
[onTouchEventReceiver performSelector:onTouchSelector];
}
}
代码产生以下结果:
2010-07-18 23:40:54.776 app[737:903] setting obj to <appCtl: 0x10fa00>
2010-07-18 23:40:54.787 app[737:903] ====----- <appCtl: 0x10fa00>
...触摸屏幕后...
Got touch event at coordinates 154 x 243 , mask : 2
2010-07-18 23:41:39.342 app[737:3b03] AALayer hit test passed : <AALayer: 0x110af0>
2010-07-18 23:41:39.348 app[737:3b03] firing a selector at (null) by <AALayer: 0x110af0>
为什么会发生这种情况?代码似乎是正确的。
Whenever my method sets an object to onTouchEventReceiver, it gets lost when another method is called by another thread.
//In header
id onTouchEventReceiver;
SEL onTouchSelector;
- (void) setOnTouchSelector:(SEL)sel withObject:(id)obj
{
NSLog(@"setting obj to %@",obj);
onTouchSelector = sel;
[self setOnTouchEventReceiver:obj];
NSLog(@"====----- %@",onTouchEventReceiver); //That works
}
//Another thread calls this
- (void) touchEventReceived
{
NSLog(@"firing a selector at %@ by %@",onTouchEventReceiver,self);
//Why on earth does that happen?????
if (onTouchEventReceiver != nil) //onTouchEventReceiver is (null)
{
[onTouchEventReceiver performSelector:onTouchSelector];
}
}
The code produces the following:
2010-07-18 23:40:54.776 app[737:903] setting obj to <appCtl: 0x10fa00>
2010-07-18 23:40:54.787 app[737:903] ====----- <appCtl: 0x10fa00>
... after the screen was touched ...
Got touch event at coordinates 154 x 243 , mask : 2
2010-07-18 23:41:39.342 app[737:3b03] AALayer hit test passed : <AALayer: 0x110af0>
2010-07-18 23:41:39.348 app[737:3b03] firing a selector at (null) by <AALayer: 0x110af0>
Why does that happen? The code seems to be correct .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的日志消息中的
AALayer
让我认为它可能是CALayer
的子类。如果是这种情况,那么您可能很容易拥有两个独立的对象。 CALayer 被复制以供渲染机器呈现。如果这是问题所在,您需要子类-initWithLayer:
以适当地复制额外的 ivars。The
AALayer
in your log message makes me think that it might be a subclass ofCALayer
. If this is the case, then you might easily have two separate objects.CALayer
s get duplicated for presentation by the rendering machinery. If this is the problem, you need to subclass-initWithLayer:
to copy your extra ivars appropriately.我想你忘记了@property。您可以:
1/ 显示属性代码
2/ 将此行更改为:
NSLog(@"====----- %@",onTouchEventReceiver); //这有效
到NSLog(@"====----- %@", [self getOnTouchEventReceiver]);
//这有效I think yo forgot the @property. Can you :
1/ Show me the property code
2/ Change this line to :
NSLog(@"====----- %@",onTouchEventReceiver); //That works
toNSLog(@"====----- %@", [self getOnTouchEventReceiver]);
//That works