Swift
Xcode 6
Button Text
iOS Development
Coding Tutorial

How to change button text in Swift Xcode 6?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Changing a button’s text in Swift is usually a one-line operation, but the correct API matters. With UIButton, you should normally use setTitle(_:for:) rather than writing directly to titleLabel?.text, because button titles are stored per control state.

The Correct Way to Change Button Text

For the normal state, use:

swift
myButton.setTitle("Save", for: .normal)

That updates the title the button shows in its default state. Here is a complete example inside a view controller:

swift
1import UIKit
2
3final class ViewController: UIViewController {
4    @IBOutlet weak var myButton: UIButton!
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        myButton.setTitle("Start", for: .normal)
9    }
10
11    @IBAction func buttonTapped(_ sender: UIButton) {
12        sender.setTitle("Tapped", for: .normal)
13    }
14}

If the button is connected from a storyboard, that is usually all you need.

Why titleLabel?.text Is the Wrong Shortcut

Beginners often try this:

swift
myButton.titleLabel?.text = "Save"

That can appear to work in some situations, but it is not the right public API for updating titles. UIButton manages titles for states such as:

  • '.normal'
  • '.highlighted'
  • '.selected'
  • '.disabled'

Using setTitle(_:for:) keeps that state model intact.

Setting Different Titles for Different States

Buttons can show different text depending on state:

swift
myButton.setTitle("Submit", for: .normal)
myButton.setTitle("Submitting...", for: .disabled)
myButton.setTitle("Selected", for: .selected)

This is useful for forms, toggles, or long-running operations where the UI should reflect progress.

For example:

swift
1@IBAction func submit(_ sender: UIButton) {
2    sender.isEnabled = false
3    sender.setTitle("Submitting...", for: .disabled)
4
5    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
6        sender.isEnabled = true
7        sender.setTitle("Done", for: .normal)
8    }
9}

Storyboard and Interface Builder

If you want an initial title without code, set the button’s Title field in Interface Builder. Then update it later in code only when the app logic changes.

The typical flow in Xcode is:

  1. drag a button onto the storyboard
  2. set an initial title in the Attributes Inspector
  3. create an IBOutlet if code needs to change it later
  4. create an IBAction if a tap should trigger the change

That split keeps static setup in the storyboard and dynamic behavior in Swift.

A Note About Old Swift Syntax

The title mentions Xcode 6, so you may still see legacy examples such as:

swift
myButton.setTitle("Save", forState: UIControlState.Normal)

That was older Swift syntax. In modern Swift, the equivalent is:

swift
myButton.setTitle("Save", for: .normal)

The intent is the same. If you are maintaining older code, the concepts still transfer directly.

Localization-Friendly Titles

If the app supports multiple languages, do not hard-code user-facing strings all over the codebase. Use localized strings:

swift
myButton.setTitle(NSLocalizedString("save_button", comment: "Save action"), for: .normal)

That keeps button titles consistent with the rest of the app’s localization workflow.

Common Pitfalls

The most common mistake is using titleLabel?.text instead of setTitle(_:for:). It bypasses how UIButton is designed to manage titles.

Another issue is updating the wrong state. If you set the title for .normal but the button is disabled, the user may still see the disabled-state title instead.

Developers also forget to connect the IBOutlet or IBAction in Interface Builder, which makes the button appear “unchangeable” when the real problem is a missing connection.

Finally, UI updates must happen on the main thread. If your button title changes after a background task, dispatch the update back to the main queue.

Summary

  • Use setTitle(_:for:) to change a UIButton title.
  • Prefer .normal for the default visible title unless you specifically need another control state.
  • Avoid writing to titleLabel?.text directly.
  • Storyboards are fine for initial titles; code is best for dynamic updates.
  • Legacy Xcode 6 syntax maps directly to the same modern UIButton behavior.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.