简体   繁体   English

如何使用IBAction方法调用加速器方法?

[英]How to call accelerator method using IBAction method?

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: 我想添加一个启动按钮来启动加速度计,如何在我使用的代码中设置IBAction来做到这一点:

-(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! 我确定如何调用该方法有问题,但不确定是什么!

you have to write your accelerometer initialization code in the IBAction function. 您必须在IBAction函数中编写加速度计初始化代码。 remove it from ViewDidLoad. 从ViewDidLoad中删除它。 now accelerometer works after the button pressed. 现在,按下按钮后,加速度计即可工作。

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM