【问题标题】:Changing UIButton text更改 UIButton 文本
【发布时间】:2012-07-10 03:38:21
【问题描述】:

所以我试图在单击 UIButton 时更新它的文本。我正在使用以下行来更改文本:

calibrationButton.titleLabel.text = @"Calibration";

我已验证文本正在更改,但是当我运行应用程序并单击按钮时,它会在瞬间更改为“校准”,然后立即恢复为默认值。任何想法为什么会发生这种情况?我需要调用某种刷新函数吗?

【问题讨论】:

标签: objective-c ios xcode event-handling uibutton


【解决方案1】:

在布置其子视图时,UIButton 将使用其自己的标题值设置其 titleLabel 的文本值,以便您可以为四种状态(正常、突出显示、选中、禁用)设置最多四个不同的字符串。

因为这个特性,直接设置titleLabel的文本不会持久化,会在布局子视图时被按钮重置。

这是更改按钮状态的标题文本所必须执行的操作。

[calibrationButton setTitle:@"Calibration" forState:UIControlStateNormal];

【讨论】:

  • 那么 button.titleLabel.text=@"some text" 应该怎么做呢?
  • @BorisGafurov 如果您输入button.titleLabel,您会看到titleLabel 是一个只读属性,因此任何只读属性的属性也是只读的。使用点符号来编辑它们是行不通的,因此您需要显式的方法来编辑它们。这至少对我来说是有意义的。
  • 我想我浪费了大约两个小时才意识到这一点(遗憾的是我想我已经知道了!)
  • 感谢您的回答。
  • @erdekhayser 实际上,只读属性 CAN 具有读/写属性。我使用 titlelabel 的标签将整数传递给操作方法。另请参阅 Apple 的文档 link Although this property is read-only, its own properties are read/write. Use these properties primarily to configure the text of the button. For example:
【解决方案2】:

要设置按钮文本,请使用以下方法:

[calibrationButton setTitle: @"Calibration" forState: UIControlStateNormal];

请参阅UIButton 类参考以获取更多详细信息... http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html

或者在 Swift 3 中:

calibrationButton.setTitle("Calibration", for: .normal)

【讨论】:

    【解决方案3】:

    您可以通过编程方式设置按钮标题,如下所示:

    [myButton setTitle:@"buttonTitle" forState:UIControlStateNormal];
    

    您还可以从情节提要中设置按钮标题属性。

    【讨论】:

      【解决方案4】:

      没什么大不了的,而且可能很明显,但是按钮有几种状态可用。如果您提供了“错误”的内容,您将不会看到所需的文本更改。

      我注意到我的按钮没有显示我添加的文本,使用此处显示的方法。检查此链接以确保您提供了您想要的 UIControlState。

      What's the difference between UIControlStateHighlighted and UIControlStateSelected?

      【讨论】:

        【解决方案5】:

        对于 Swift 3.0

        let button = UIButton(type: .system)
        button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        button.setTitle("set here", for: .normal)
        button.addTarget(self, action: #selector(TableViewController.actionButtonTocuh), for: .touchUpInside)
        button.titleLabel?.textColor  = #colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1)
        view.addSubview(button)
        

        【讨论】:

          【解决方案6】:

          如果您不想为所有状态设置标题,只需将其设置为正常状态即可,因为未设置状态的标题将默认为正常状态的标题。

          btn.setTitle("Some text", for:.normal)
          

          【讨论】:

            【解决方案7】:

            对于 Swift 2.0:

            let btnObject : UIButton  = UIButton() 
            btnObject.frame = CGRect(x: 8, y: 89, width: 70, height: 22)
            btnObject.setTitle("Button Title", forState: UIControlState.Normal)
            btnObject.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 13)
            btnObject.titleLabel?.textColor = UIColor.whiteColor()
            btnObject.backgroundColor = UIColor(red: 189/255, green: 176/255, blue: 0/255, alpha: 1)
            btnObject.titleLabel?.textAlignment = NSTextAlignment.Center
            btnObject.addTarget(self, action: "btnbtnObjectClick:", forControlEvents: UIControlEvents.TouchUpInside)
            subView.addSubview(btnObject)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-10-15
              • 2011-07-31
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多