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 ......didAcceler开发者_如何学Goate:(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?
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
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?
精彩评论