【问题标题】:How to change button text in Swift Xcode 6?如何在 Swift Xcode 6 中更改按钮文本?
【发布时间】:2020-11-18 19:16:22
【问题描述】:

这就是我想要做的。如果您曾经玩过 Halo 或 CoD,您就会知道您可以更改武器装载的名称。

我正在做的是让您可以使用文本字段更改加载名称。这就是问题所在,加载菜单中的加载名称是一个按钮(用于选择和查看有关该加载的信息),我可以这样写:

@IBAction func renameClassButton(sender: AnyObject) {
    classTopButton.text = "\(classTopTextField)"
}

除了 [classTopButton] 是一个不允许使用 '.text' 后缀的按钮

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    你可以这样做:

    button.setTitle("my text here", forState: .normal)
    

    Swift 3 和 4:

    button.setTitle("my text here", for: .normal)
    

    【讨论】:

    • 或者更短一点。你可以省略UIControlState 部分,只写button.setTitle("my text here", forState: .Normal)
    • 如果您使用 Storyboard,我认为提及:buttonIBOutlet(与包含这行代码的 IBAction 形成对比)会有所帮助。
    【解决方案2】:

    在 Xcode 8 - Swift 3 中:

    button.setTitle( "entertext" , for: .normal )

    【讨论】:

    • 第二个参数现在是.normal,而不是.Normal
    • Capital .Normal 现在会报错。小写现在是正确的。 .normal
    • 我必须说原始答案是:button.setTitle( "entertext" , for: .Normal ) 它是支持 swift 3.0 的第一版 Xcode 的工作变体
    【解决方案3】:

    现在是 swift 3,

        let button = (sender as AnyObject)
        button.setTitle("Your text", for: .normal)
    

    (变量的常量声明不是必需的,只需确保您使用按钮中的发送者,就像这样):

        (sender as AnyObject).setTitle("Your text", for: .normal)
    

    请记住,这是在按钮的 IBAction 中使用的。

    【讨论】:

      【解决方案4】:

      注意:

      someButton.setTitle("New Title", forState: .normal)
      

      仅在标题类型为 Plain 时有效。

      【讨论】:

      【解决方案5】:

      swift 4 和 3 一样工作

      libero.setTitle("---", for: .normal)
      

      libero 是一个 uibutton

      【讨论】:

        【解决方案6】:

        您可以使用发件人参数

          @IBAction func TickToeButtonClick(sender: AnyObject) {
        
                sender.setTitle("my text here", forState: .normal)
        
        
        }
        

        【讨论】:

        • 它是 Xcode 第一个版本的工作变体,稍后支持 swift 3.0。Normal 变为 .normal
        【解决方案7】:

        请注意,如果您使用的是 NSButton,则没有 setTitle func,而是一个属性。

        @IBOutlet weak var classToButton: NSButton!
        
        . . .
        
        
        classToButton.title = "Some Text"
        

        【讨论】:

          【解决方案8】:

          在 Swift 4 中,我之前尝试过所有这些,但只运行:

          @IBAction func myButton(sender: AnyObject) {
             sender.setTitle("This is example text one", for:[])
             sender.setTitle("This is example text two", for: .normal)
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-12
            • 1970-01-01
            • 2015-11-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-03-06
            • 2015-01-16
            相关资源
            最近更新 更多