xcode 创建一个可以触发两个操作并更改其标题的按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11069663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:37:30  来源:igfitidea点击:

Create a single button which can trigger two actions and changes it's title?

iosxcodebuttonuibuttoncore-motion

提问by sam

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];

I want to create a button that can start and stop accelerometer updates as user presses it, But it's title too should change from start to stop and also it should continues until user reset everything by an anoterh button, but i have no idea of getting it done, except the following code, But it doesnt change the title and call stop action?

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

Thank you in advance!

先感谢您!

回答by Pfitz

Manage the state of the button in some kind of was eg. with a @property and use just one action:

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

in you .h

在你.h

@property (nonatomic) BOOL startStopButtonIsActive;

in your .m

在你的.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
    }
} 

回答by Eitan

You are close.

你很近。

Add only one target to the button - such that the target method that has awareness of the current state. Make sure you have a colon in the selector so it receives the sender as an argument of the method. i.e. startOrStopAccel:versus startOrStopAccel.

只向按钮添加一个目标 - 这样目标方法就知道当前状态。确保选择器中有一个冒号,以便它接收发件人作为方法的参数。即startOrStopAccel:startOrStopAccel.

You can have that one target method call either start_Accelor stop_Acceldepending on the state.

你可以有一个目标的方法调用要么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];
    }
 }

回答by Chris Trahey

There should only be one target in this case, and you should have it pass the button along with the action by adding an argument to the signature:

在这种情况下应该只有一个目标,并且您应该通过向签名添加参数来让它与操作一起传递按钮:

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

Inside that method, you can check state and change state, as well as the properties of the button (which is referenced in 'sender'.

在该方法中,您可以检查状态和更改状态,以及按钮的属性(在 'sender'.

回答by Senthilkumar

first you want declare your button and an integer variable in .h file.

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

then ,

然后 ,

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];
}