skip to Main Content

How could I change the button title in swift?

I’ve tried the below code but it’s not working:

button.setTitle("myTitle", for: .Normal)

2

Answers


  1. @IBOutlet weak var defaultButton: UIButton! 
    override func viewDidLoad() {
            super.viewDidLoad()
            defaultButton.setTitle("hello", for: .normal)
        }
    

    // I Set button title in didload and further changed on button action
    //button is so called as defaultButton

      @IBAction func addButtonTapped(_ sender: Any) {
            defaultButton.setTitle("myTitle", for: UIControl.State.normal) 
    //defaultButton.setTitle("myTitle", for: .normal)
        }
    

    The both methods are working fine !!

    Login or Signup to reply.
  2. if your button is an @IBOutlet and have proper attribute connection then the following line will work

    button.setTitle("myTitle", for: .normal)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search