简体   繁体   中英

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

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM