无法在ObjC中设置对象?

时间:2010-07-18 22:33:10

标签: iphone objective-c selector nsobject

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

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

2 个答案:

答案 0 :(得分:2)

日志消息中的AALayer让我觉得它可能是CALayer的子类。如果是这种情况,那么您可能很容易拥有两个单独的对象。 CALayer被复制以供呈现机制呈现。如果这是问题,您需要继承-initWithLayer:以适当地复制额外的ivars。

答案 1 :(得分:0)

我想你忘记了@property。你可以:

1 /显示属性代码

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