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.
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:
That updates the title the button shows in its default state. Here is a complete example inside a view controller:
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:
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:
This is useful for forms, toggles, or long-running operations where the UI should reflect progress.
For example:
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:
- drag a button onto the storyboard
- set an initial title in the Attributes Inspector
- create an
IBOutletif code needs to change it later - create an
IBActionif 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:
That was older Swift syntax. In modern Swift, the equivalent is:
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:
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 aUIButtontitle. - Prefer
.normalfor the default visible title unless you specifically need another control state. - Avoid writing to
titleLabel?.textdirectly. - Storyboards are fine for initial titles; code is best for dynamic updates.
- Legacy Xcode 6 syntax maps directly to the same modern
UIButtonbehavior.
Related reading
- How to change Button Title Alignment in Swift?
- How to change color of Android ListView separator line?
- how to change color of textview hyperlink?
- How to change color of the back arrow in the new material theme?
- How to change color of the back arrow in the new material theme?
- How to change colors of a Drawable in Android?
- How to change display name of an app in react-native
- How to change font of UIButton with Swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.