0

I want to add a start button which will start the accelerometer, how can I set an IBAction to do that, in my code I use:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    x.text = [NSString stringWithFormat:@"X is: %f", acceleration.x];
    y.text = [NSString stringWithFormat:@"Y is: %f", acceleration.y];
    z.text = [NSString stringWithFormat:@"Z is: %f", acceleration.z];
NSLog (@"Y = @%f", y.text);
}

-(IBAction)startPedometerPressed {
    [startButton accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration];

}

This returns an error because of the undeclared identifier 'accelerometer'.

I'm sure there is something wrong in how I call the method but not sure what is it!

4

1 に答える 1

0

IBAction 関数に加速度計の初期化コードを記述する必要があります。ViewDidLoad から削除します。ボタンを押した後に加速度計が動作するようになりました。

-(IBAction)startPedometerPressed 
{
     [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
     [[UIAccelerometer sharedAccelerometer] setDelegate:self];
}

お役に立てれば。

于 2012-08-04T10:33:42.893 に答える