无法在 ObjC 中设置对象?

发布于 2024-09-10 13:14:44 字数 1258 浏览 6 评论 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>

为什么会发生这种情况?代码似乎是正确的。

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 技术交流群。

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

发布评论

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

评论(2

紫竹語嫣☆ 2024-09-17 13:14:44

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

The AALayer in your log message makes me think that it might be a subclass of CALayer. If this is the case, then you might easily have two separate objects. CALayers get duplicated for presentation by the rendering machinery. If this is the problem, you need to subclass -initWithLayer: to copy your extra ivars appropriately.

岁月如刀 2024-09-17 13:14:44

我想你忘记了@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 to NSLog(@"====----- %@", [self getOnTouchEventReceiver]); //That works

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