Accelerometer Delegate in different Class

Discussion in 'iOS Programming' started by man2manno, May 11, 2009.

  1. man2manno macrumors member

    Mar 21, 2009
    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 question is how do I get that to work in a class that I created?

  2. BlackWolf macrumors regular

    Apr 9, 2009
    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)
  3. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    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.

Share This Page