简体   繁体   中英

iPhone Accelerometer crashes app

I have an navigation-based app that I want to use the accelerometer to detect a shake and cause a scroll view to scroll to the next page. I have added accelerometer code to the view controller of my scrollView, and it works great; a shake calls my page change method. But when I unload the scrollViewController from the navigation stack the app crashes.

I set up the accelerometer in the viewDidLoad method of the scrollViewController, and respond to a shake in the accelerometer: didAccelerate: delegate method.

When the scrollViewController gets deallocated, the app crashes.

What am I missing?

Thanks

Found the answer in another thread:

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

I needed to set the accelerometer's delegate to nil when my scrollView deallocated. The accelerometer is a singleton that retains a reference to it's delegate, in this case my scrollViewController. When my scrollViewController popped off the stack, the reference was left hanging, crashing he app.

Thanks to Phil Nash for the answer!

What is your update rate on the accelerometer? I've heard of weird things happening if it's too high.

I can't make a comment so I had to post this in the answer. can you display how you new view page load. as I am understanding is once the you shake it it suppose to switch to a new view. Can you display your call to the new view.

Simple: by setting the delegate method you have told another object (the accelerometer) that the scrollview exists.

But did you tell it when it ceased to exist, so it could no not to send any messages to it?

I guess not. You need to set the delegate again, to nil this time.

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