0

每当我的方法将一个对象设置为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>

为什么会这样?代码似乎是正确的。

4

2 回答 2

2

您的AALayer日志消息中的 让我认为它可能是CALayer. 如果是这种情况,那么您可能很容易拥有两个单独的对象。CALayers 被渲染机器复制以供呈现。如果这是问题所在,则需要子类-initWithLayer:化以适当地复制额外的 ivars。

于 2010-07-19T01:10:19.913 回答
0

我想你忘记了@property。你能 :

1/ 显示房产代码

2/ 将此行更改为 : NSLog(@"====----- %@",onTouchEventReceiver); //That worksNSLog(@"====----- %@", [self getOnTouchEventReceiver]);// 有效

于 2010-07-19T01:20:51.497 回答