How to change button text in Swift Xcode 6?

前端 未结 8 802
囚心锁ツ
囚心锁ツ 2021-01-30 03:48

Here\'s what I\'m trying to do. If you\'ve ever played Halo or CoD, you\'d know that you could change the name of a weapon load-out.

What I\'m doing is making it so you

相关标签:
8条回答
  • 2021-01-30 04:03

    It is now this For swift 3,

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

    (The constant declaration of the variable is not necessary just make sure you use the sender from the button like this) :

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

    Remember this is used inside the IBAction of your button.

    0 讨论(0)
  • 2021-01-30 04:03

    swift 4 work as well as 3

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

    where libero is a uibutton

    0 讨论(0)
  • 2021-01-30 04:07

    You can Use sender argument

      @IBAction func TickToeButtonClick(sender: AnyObject) {
    
            sender.setTitle("my text here", forState: .normal)
    
    
    }
    
    0 讨论(0)
  • 2021-01-30 04:18

    In Swift 4 I tried all of this previously, but runs only:

    @IBAction func myButton(sender: AnyObject) {
       sender.setTitle("This is example text one", for:[])
       sender.setTitle("This is example text two", for: .normal)
    }
    
    0 讨论(0)
  • 2021-01-30 04:20

    NOTE:

    line

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

    works only when Title type is Plain.

    0 讨论(0)
  • 2021-01-30 04:23

    In Xcode 8 - Swift 3:

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

    0 讨论(0)
提交回复
热议问题