0
CGRect buttonFrame = CGRectMake( 10, 80, 100, 30 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: @"Start" forState: UIControlStateNormal];
[button addTarget:self action:@selector(start_Accel) forControlEvents:UIControlEventTouchUpInside];
[button setTitle: @"Stop" forState: UIControlEventTouchUpInside];
[button addTarget:self action:@selector(stop_Accel) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];

我想创建一个按钮,可以在用户按下它时启动和停止加速度计更新,但它的标题也应该从开始到停止更改,并且它应该继续直到用户通过另一个按钮重置所有内容,但我不知道得到它完成,除了以下代码,但它不会改变标题并调用停止动作?

先感谢您!

4

4 回答 4

4

以某种方式管理按钮的状态,例如。使用 @property 并仅使用一个操作:

在你.h

@property (nonatomic) BOOL startStopButtonIsActive;

在你的 .m

@synthesize startStopButtonIsActive = _startStopButtonIsActive;

 ....

- (void)viewDidLoad {
       [button addTarget:self action:@selector(startStopButtonPressed) forControlEvents:UIControlEventTouchUpInside];
      // other stuff
}

- (void)startStopButtonPressed {
    if (startStopButtonIsActive) {
        self.startStopButtonIsActive = !self.startStopButtonIsActive  //toggle!
        // do your stuff here
    } else {
        self.startStopButtonIsActive = !self.startStopButtonIsActive  //toggle!
        // do your stuff here
    }
} 
于 2012-06-17T07:37:21.790 回答
1

你很亲密。

仅向按钮添加一个目标 - 使目标方法能够感知当前状态。确保选择器中有一个冒号,以便它接收发送者作为方法的参数。即startOrStopAccel:startOrStopAccel.

您可以根据状态调用一个目标方法,start_Accel也可以调用该方法。stop_Accel

-(void) viewDidLoad {
    //...
    //...
    CGRect buttonFrame = CGRectMake( 10, 80, 100, 30 );
    UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
    [button setTitle: @"Start" forState: UIControlStateNormal];
    [button addTarget:self action:@selector(startOrStopAccel:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
}

-(void)startOrStopAccel:(UIButton*)sender {
    if ([sender.title isEqualToString:@"Start"]) {
        [sender setTitle: @"Stop" forState: UIControlStateNormal];
        [self start_Accel];
    }
    else {
        [sender setTitle: @"Start" forState: UIControlStateNormal];
        [self stop_Accel];
    }
 }
于 2012-06-17T07:51:05.460 回答
0

在这种情况下应该只有一个目标,您应该通过在签名中添加一个参数来让它传递按钮和动作:

[button addTarget:self action:@selector(accelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// the interface being:
-(void) accelButtonPressed:(UIButton *) sender;

在该方法中,您可以检查状态和更改状态,以及按钮的属性(在“sender”中引用)。

于 2012-06-17T07:38:43.073 回答
0

首先,您要在 .h 文件中声明您的按钮和一个整数变量。

然后 ,

intergervariable=0;

    button = [[UIButton alloc] initWithFrame: buttonFrame];
    [button setTitle: @"Start" forState: UIControlStateNormal];
    [button addTarget:self action:@selector(start_Accel) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];

after that inside the start_Accel function check like this


if(intergervariable==0)
{
intergervariable=1; 
[button setTitle: @"Start" forState: UIControlStateNormal];
}
else if(intergervariable==1)
{
intergervariable=0;
 [button setTitle: @"Stop" forState: UIControlStateNormal];
}
于 2012-06-17T07:40:33.940 回答