PDA

View Full Version : Accelerometer Delegate in different Class




man2manno
May 11, 2009, 03:07 PM
Hey guys, still somewhat new at this so I could use a little help. I am trying to get the accelerometer to work in a class i created ("myclass"). I put the following code in my applicationDidFinishLauching method in my appDelegate:

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60.0f)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
NSLog(@"Accelerometer configured and started");

Then I put the following code under it:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate: (UIAcceleration *)acceleration
{

if ([acceleration x] >=2 || [acceleration y] >=2 || [acceleration z] >=2)
{
NSLog(@"X=%f Y=%f Z=%f", [acceleration x], [acceleration y], [acceleration z]);
}
}

That all works perfectly...my question is how do I get that to work in a class that I created?

Thanks



BlackWolf
May 11, 2009, 03:27 PM
I don't even understand the question :D
you set a delegate. the messages the Accelerometer sends are sent to that delegate. if you need to receive accelerations in a class you created, then set an instance of that class as the delegate of the accelerometer - or, much more simple, execute the exact code you posted when the instance of your class is created (in the init method)

dejo
May 11, 2009, 05:04 PM
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
If you have that in your application-delegate, that means you've made the application-delegate the delegate of the accelerometer. That is, self = application-delegate.