views:

469

answers:

5

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

A: 

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

Nosredna
update rate = 1/30 sec
Alpinista
OK. That's a safe number. I've heard that people sometimes have crashes when it's over 100 hertz.
Nosredna
A: 

How are you switching pages? Do you deallocate your only view controller, then allocate a new one?

ryansstack
This would be better as a comment on the original question.
unforgiven3
I am just scrolling between subviews added to to the scrollView. The app crashes when the scrollView is poped off the stack by clicking the back button on the navigation bar
Alpinista
A: 

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.

JuniorFlip
+2  A: 

Found the answer in another thread:

http://stackoverflow.com/questions/740519/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!

Alpinista
A: 

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.

Alex Brown