Search code examples
iphoneaccelerometer

How can the accelerometer be used in a controller with a separate view?


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?


Solution

  • 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