Pedro
Pedro

Reputation: 507

Button text reverts when tapped

I have a button that throughout the program he changes his name.
original name is "line".
is then renamed to "bar".

When is named "bar" and I press it executes the following code.
in the code is the variable _bt3.

[UIView animateWithDuration:1
                          delay:0
                        options:UIViewAnimationCurveEaseOut
                     animations:^{
                         _bt1.transform = CGAffineTransformMakeTranslation(0,0);
                         _viewBt1.transform = CGAffineTransformMakeTranslation(0,0);
                         _bt2.transform = CGAffineTransformMakeTranslation(0,0);
                         _viewBt2.transform = CGAffineTransformMakeTranslation(0,0);
                         _bt3.transform = CGAffineTransformMakeTranslation(0,-_viewBt3.frame.size.height+68);
                         _viewBt3.transform = CGAffineTransformMakeTranslation(0,-_viewBt3.frame.size.height+68);
                         _bt4.transform = CGAffineTransformMakeTranslation(0,0);
                         _viewBt4.transform = CGAffineTransformMakeTranslation(0,0);

                     }
                     completion:^(BOOL finished) {
                     }];

after that magically appears with your first name.

I just change the name of this button is pressed when the _bt2. I do not use any more part of code because the first name comes from the storyboard

when _bt2 is pressed runs the following code

- (IBAction)bt2Pressed:(id)sender {
    NSLog(@"botao2");
    _bt3.titleLabel.text=@"Bar";
}

anyone know how to solve?

Upvotes: 0

Views: 259

Answers (1)

rob mayoff
rob mayoff

Reputation: 385540

The problem is that you are setting _bt3.titleLabel.text directly. Don't do that.

A button has states: normal, highlighted, selected, and disabled. It knows what its text should be for each state. When _bt3 changes state, it sets _bt3.titleLabel.text. This overrides your change to _bt3.titleLabel.text.

If you don't set the text for a non-normal state, the button uses the text for the normal state.

When the user touches _bt3, _bt3 changes its state to highlighted and sets _bt3.titleLabel.text to the text set for its highlighted state. When the user stops touching _bt3, _bt3 changes its state back to normal and sets _bt3.titleLabel.text to the text set for its normal state.

So instead of setting _bt3.titleLabel.text directly, you need to tell the button what text it should display in the normal state:

[_bt3 setTitle:@"Bar" forState:UIControlStateNormal];

Upvotes: 2

Related Questions