UIAccelerometer 不向第二个委托发送事件

发布于 2024-08-10 10:41:57 字数 731 浏览 5 评论 0原文

我正在开发一款游戏,每个级别使用不同的控制器。它需要通过加速度计检测震动,因此它将自己注册为委托,如下所示:

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = kUpdateInterval; 

当关卡结束时,该控制器被释放并释放。以前,在释放该控制器后我遇到了崩溃,因为我没有清除 UIAccelerometer 上的委托(即它仍在向现已释放的对象发送事件)。所以现在,在 dealloc 内部,我正在这样做:

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = nil;

一切都很好,并且第一个关卡运行顺利。当我进入下一个级别,创建一个新的级别控制器并再次运行第一批代码(现在将新的控制器设置为委托)时,问题就会发生。此后,我没有收到来自加速度计的任何电话。

所以问题是,是否只允许每个应用程序有一名加速计代表,或者我只是错过了一些东西?我在文档中没有看到任何不允许多次设置委托的内容。我对 Obj-C 有点陌生,但据我了解代表,这不应该太不正统。

注意:我知道在 3.0 中我可以只监听震动通知。不幸的是,在我对摇晃感兴趣的整个过程中,我需要其他东西来充当第一响应者。所以我不能仅仅重构该选项。

I am developing a game that uses a different controller for each level. It needs to detect a shake via the accelerometer, so it registers itself as a delegate like so:

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = kUpdateInterval; 

When the level ends, this controller gets dealloc'd and freed. Previously, I was getting a crash after this controller was freed because I didn't nil out the delegate on UIAccelerometer (i.e. it was still sending events to an object that has now been freed). So now, inside of dealloc, I'm doing this:

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = nil;

All is well and good and the first level plays without a hitch. The problem happens when I get to the next level, create a new level controller and run that first batch of code again (setting the new contoller now as the delegate). After this, I'm not getting any calls from the Accelerometer.

So the question is, are you only allowed to have one delegate per app for the Accelerometer, or am I just missing something? I haven't seen anything in the docs that disallows setting the delegate multiple times. I'm slightly new to Obj-C, but as far as I understand delegates this shouldn't be too unorthodox.

Note: I know that in 3.0 I could just listen for shake notifications. Unfortunately, I need something else to be first responder the entire time I'm interested in the shake. So I can't just refactor to that option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

简单 2024-08-17 10:41:57

一般来说,具有委托的对象一次只有一个。除非另有说明,否则在特定实例的生命周期内将多个不同对象设置为委托不应受到任何限制。

我想知道发生的情况是否是在您将第二个控制器设置为加速器委托之后,您的第一个控制器正在dealloc。您是否使用调试器观察过该代码以确保一切都按照您期望的顺序发生?

In general, objects that have delegates have only one at a time. Unless otherwise documented, there shouldn't be any limitations on setting more than one different object as delegate over the lifetime of a particular instance.

I wonder if what's happening is that your first controller is being dealloced after you set the second controller as the accelerator delegate. Have you watched that code with the debugger to be sure that everything's happening in the order you expect?

笨笨の傻瓜 2024-08-17 10:41:57

一个可能的解决方案是使用一个不同的类(也许是您的应用程序委托)充当 UIAccelerometer 的委托,并让该类在收到 accelerometer:didAccelerate: 消息时发送自定义 NSNotification。您可以仅在检测到您关心的加速类型时发送消息,也可以每次发送通知并将 UIAcceleration 参数包含在通知 userInfo 中。对加速度数据感兴趣的控制器可以根据需要注册通知。

A possible solution would be to have a different class, perhaps your app delegate, act as the delegate of the UIAccelerometer and have that class send custom a NSNotification when it receives the accelerometer:didAccelerate: message. You could either only send the message when you detect the type of acceleration you care about or you could send the notification every time and include the UIAcceleration paramter as part of the notifications userInfo. Your controllers that are interested in acceleration data can register for the notifications as needed.

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