【问题标题】:UIButton title different than expected [closed]UIButton标题与预期不同[关闭]
【发布时间】:2014-01-27 12:03:43
【问题描述】:

这是我的代码:

- (void)viewDidLoad{
     [super viewDidLoad];
     Button.titleLabel.text = [[problemSetting objectAtIndex:currentProblem] getP];
}

我在这个方法中得到了“UIButton”的文本标题。 每次给currentProblem加1,我想改变标签“UIButton”的文字

我已经构建了这个应用程序 当我触摸这个“UIButton”时,它将变为“故事板”中初始设置的文本文本。 我不想看到被设置的初始甚至触摸“UIButton”和“默认文本”

当我单击UIButton 时,我想将UIButton 的标题更改为我从problemSetting 数组中取出的文本。

谁能告诉我怎么做?

谢谢。

【问题讨论】:

  • 显示problemSetting的声明。
  • 这个问题太难理解了。清楚地告诉我们。
  • 您的具体问题是什么?
  • 这里写了什么?如果要在运行时更改 Button 的标题,可以使用 [Button setTitle:@"Any String" forState:UIControlStateNormal];

标签: ios iphone objective-c uibutton


【解决方案1】:

首先,如果您想更改 UIButton 对象的标题,您应该使用:

[btn setTitle:@"YOUR TITLE" forState:UIControlStateNormal];

要在更改 currentProblem 的值时更改标题,您可以覆盖 currentProblem 设置器(它需要是属性@property NSInteger currentProblem):

-(void)setCurrentProblem:(NSInteger)currentProblem
{
    _currentProblem = currentProblem;
    [btn setTitle:@"YOUR TITLE" forState:UIControlStateNormal];
}

如果您想在按下按钮时返回默认标题,请将此行添加到您的操作方法中:

[btn setTitle:@"DEFAULT VALUE" forState:UIControlStateNormal];

希望对您有所帮助。

【讨论】:

  • 你好 Greg。我已经解决了我的问题。非常感谢! btn setTitle:[[problemSetting objectAtIndex:currentProblem] getP]forState:UIControlStateNormal];
【解决方案2】:

试试这个可能对你有帮助,因为几天前我也遇到了同样的问题,然后我用了这个

 NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"YOUR_TITLE"];
 [UIButton setAttributedTitle:commentString forState:UIControlStateNormal];

编码愉快!!!

【讨论】:

  • 你好kagmanoj。我已经解决了我的问题。非常感谢! btn setTitle:[[problemSetting objectAtIndex:currentProblem] getP]forState:UIControlStateNormal];
猜你喜欢
  • 1970-01-01
  • 2021-08-04
  • 2020-10-20
  • 2013-02-21
  • 1970-01-01
  • 1970-01-01
  • 2013-11-14
  • 2019-12-06
  • 2011-09-03
相关资源
最近更新 更多