Reputation: 3741
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
Upvotes: 1
Views: 1288
Reputation: 42942
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.
Upvotes: 0
Reputation: 3741
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!
Upvotes: 3
Reputation: 417
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.
Upvotes: 0
Reputation: 86346
What is your update rate on the accelerometer? I've heard of weird things happening if it's too high.
Upvotes: 0