轻按按钮即可还原按钮文字 [英] Button text reverts when tapped

查看:70
本文介绍了轻按按钮即可还原按钮文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个程序中都有一个按钮,他可以更改他的名字.
原名是线".
然后重命名为"bar".

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

当被命名为"bar"并且按下它时,将执行以下代码.
代码中的变量是_bt3.

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.

我只是更改了_bt2时按下此按钮的名称. 我不再使用任何代码部分,因为名字来自情节提要

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

按下_bt2时,将运行以下代码

when _bt2 is pressed runs the following code

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

有人知道如何解决吗?

推荐答案

问题是您直接设置了_bt3.titleLabel.text.不要那样做.

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

一个按钮具有以下状态:正常,突出显示,选中和禁用.它知道每个州的文字应该是什么. _bt3更改状态时,将设置_bt3.titleLabel.text.这将覆盖您对_bt3.titleLabel.text所做的更改.

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.

当用户触摸_bt3时,_bt3会将其状态更改为突出显示,并将_bt3.titleLabel.text设置为其突出显示状态的文本集.当用户停止触摸_bt3时,_bt3会将其状态恢复为正常,并将_bt3.titleLabel.text设置为正常状态的文本集.

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.

因此,无需直接设置_bt3.titleLabel.text,而是需要告诉按钮在正常状态下应显示什么文本:

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

这篇关于轻按按钮即可还原按钮文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆