views:

207

answers:

2

I am trying to get the accelerometer to detect shakes in a separate view and the accelerometer method does not get called?

When I place code to set the accelerometer attributes in the controllers viewDidLoad method and add the ......didAccelerate:(UIAcceleration *) acceleration in the implementation file for the controller shakes are detected.

I'm a novice to iphone development and think that perhaps the code:

accelerometer.delegate = self;

should point to the view? Rather than self?

Any help would be really appreciated. I'm really not sure how to solve this?

A: 

There often seems to be a cognitive disconnect between an API reference and actually using the API in actual code with Cocoa/Objective-C by beginners.

This sample code shows how a view controller can access the accelerometer in an iPhone OS app.

iPhone Dev Site link

Hint: did you set an update interval?

ExitToShell
Thanks for your help. I can get the accelerometer working in a controller class file but not in a separate UIView object. I'm sure i'm missing something really obvious.The update interval is set as:accelerometer.updateInterval = 1.0f/60.0f;
jack smith
Finally got it working. Removed the UIView and placed all code in the controller. Not the best solution but it works and will re-factor later. Thanks for all your help :)
jack smith
A: 

If your accelerometer callback method is not being called there's a good chance your application delegate (or other custom class) is not supporting the UIAccelerometerDelegate class. Some very loose pseudocode on making that delcaration in your application delegate...

@interface MyApplicationDelegate: UIApplicationDelegate<UIAccelerometerDelegate>
{
   .. class definition stuff ..
}

See the Apple documentation here...

http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedFeatures/AdvancedFeatures.html

And review the Accessing Accelerometer Events section

Rob Segal
Thanks Rob :)I'm getting there and hopefully should have it working soon. The problem is I am have a separate view and a controller and have assigned the <UIAccelerometerDelegate> to UIView and the didAccelerate method to execute my code. I'm not sure where to set the accelerometer interval and delegate as UIView does not support viewDidLoad method.Thanks again for your help.
jack smith
Finally got it working. Removed the UIView and placed all code in the controller. Not the best solution but it works and will re-factor later. Thanks for all your help :)
jack smith
Glad I could be of some help and glad things are finally working. What controller are you using to implement all your code in? Is it the navigation controller or a view controller?
Rob Segal
I put all the code in a view controller and removed the separate UIView object. My knowledge of iPhone development is still quite limited but am getting there and thanks again :)
jack smith
You're welcome. Don't worry about just starting out. We are all learning. Some are just further along the path than others.
Rob Segal