How do you release a shared accelerator instance on the iPhone?

…衆ロ難τιáo~ 提交于 2019-12-22 00:22:29

问题


I have created an accelerometer variable:

UIAccelerometer   *objAccelerometer;

that I am associating to the sharedAccelerometer instance:

objAccelerometer = [UIAccelerometer sharedAccelerometer];
objAccelerometer.delegate = self;

When I release this view (to load a different view), the accelerometer instance causes the program to die. (If I have it commented out, I can switch between views without a problem).

I had a similar problem with an NSTimer, but once I called:

[myTimer invalidate];

(prior to releasing and switching the views) everything worked fine.

How should I properly release the delegate or deallocate or release the accelerometer?


回答1:


You don't release the shared accelerometer - you never retained it (and shouldn't) - it's a singleton.

The accelerometer has a reference to your object. For it to release it just set it's delegate property to nil (because it's a property it will release its reference to your object).



来源:https://stackoverflow.com/questions/740519/how-do-you-release-a-shared-accelerator-instance-on-the-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!