Skip to main content
TopMiniSite

Back to all posts

How to Get Text From Button Clicked In Swift?

Published on
4 min read
How to Get Text From Button Clicked In Swift? image

Best Swift Programming Guides to Buy in February 2026

1 iOS 26 Programming for Beginners: A hands-on guide to kickstarting your iOS app development journey with Swift 6, UIKit, and Xcode 26

iOS 26 Programming for Beginners: A hands-on guide to kickstarting your iOS app development journey with Swift 6, UIKit, and Xcode 26

BUY & SAVE
Save 20%
iOS 26 Programming for Beginners: A hands-on guide to kickstarting your iOS app development journey with Swift 6, UIKit, and Xcode 26
2 Mastering Swift 6: Modern programming techniques for high-performance apps in Swift 6.2

Mastering Swift 6: Modern programming techniques for high-performance apps in Swift 6.2

BUY & SAVE
Save 20%
Mastering Swift 6: Modern programming techniques for high-performance apps in Swift 6.2
3 Modern Swift Programming: From Fundamentals to Building Your First Apple Apps

Modern Swift Programming: From Fundamentals to Building Your First Apple Apps

BUY & SAVE
Modern Swift Programming: From Fundamentals to Building Your First Apple Apps
4 Swift Programming: A Detailed Guide to Learning Essential Concepts and Mastering Advanced Techniques

Swift Programming: A Detailed Guide to Learning Essential Concepts and Mastering Advanced Techniques

BUY & SAVE
Swift Programming: A Detailed Guide to Learning Essential Concepts and Mastering Advanced Techniques
5 iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success

iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success

BUY & SAVE
iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success
6 Swift Programming for Beginners

Swift Programming for Beginners

BUY & SAVE
Swift Programming for Beginners
7 Learning Swift: Building Apps for macOS, iOS, and Beyond

Learning Swift: Building Apps for macOS, iOS, and Beyond

BUY & SAVE
Save 52%
Learning Swift: Building Apps for macOS, iOS, and Beyond
8 Swift Cookbook: Proven recipes for developing robust iOS applications with Swift 5.9

Swift Cookbook: Proven recipes for developing robust iOS applications with Swift 5.9

BUY & SAVE
Swift Cookbook: Proven recipes for developing robust iOS applications with Swift 5.9
9 Head First Swift: A Learner's Guide to Programming with Swift

Head First Swift: A Learner's Guide to Programming with Swift

BUY & SAVE
Save 51%
Head First Swift: A Learner's Guide to Programming with Swift
+
ONE MORE?

To get text from a button clicked in Swift, you can access the button's title property or use a tag to identify the button and retrieve the text associated with it.

How do I obtain the text from a button tap event in Swift?

To obtain the text from a button tap event in Swift, you can access the button's titleLabel property in the button tap event handler method.

Here is an example code snippet that demonstrates how to obtain the text from a button tap event in Swift:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    
    let button = UIButton(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
    button.setTitle("Tap Me", for: .normal)
    button.setTitleColor(.black, for: .normal)
    button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
    
    view.addSubview(button)
}

@objc func buttonTapped(sender: UIButton) {
    if let buttonText = sender.titleLabel?.text {
        print("Button text: \\(buttonText)")
    }
}

}

In this code, a button is created and added to the view. The button's titleLabel property is accessed in the buttonTapped method to retrieve the text of the button when it is tapped.

How to retrieve the text of a button after it is clicked in Swift?

In Swift, you can retrieve the text of a button after it is clicked by creating an IBAction for the button and then accessing the title property of the button in the action method.

Here's an example code snippet:

@IBAction func buttonClicked(_ sender: UIButton) { let buttonText = sender.title(for: .normal) print("Button text: \(buttonText)") }

In this code, the buttonClicked method is called when the button is clicked. The sender parameter is the button that was clicked, and you can access its text using the title method with the .normal control state.

You can replace the print statement with any other logic you need to perform using the button text.

How to extract button text in Swift upon click event?

To extract the button text in Swift upon a click event, you can use the following steps:

  1. Create an IBAction for the button click event in your View Controller class.
  2. Inside the IBAction function, you can use the sender parameter to access the button that was clicked.
  3. Use the title property of the button to extract the text of the button.

Here is an example code snippet:

@IBAction func buttonClicked(_ sender: UIButton) { if let buttonText = sender.title(for: .normal) { print("Button Text: \(buttonText)") } }

In this code snippet, the buttonClicked IBAction function is called whenever a button is clicked. The sender parameter is used to access the button that triggered the event, and the title(for:) method is used to extract the text of the button. This text is then printed to the console.

How to get the text of a button in Swift?

To get the text of a button in Swift, you can access the titleLabel property of the button and extract the text from it. Here is an example:

let buttonText = yourButton.titleLabel?.text print(buttonText)

Replace yourButton with the actual reference to your button. This code snippet should print the text of the button to the console.

What is the method to get text from a button clicked in Swift?

In Swift, you can get the text from a button clicked by adding a target action to the button that handles the click event and then accessing the title property of the button inside the action method. Here's an example:

// Create a button let button = UIButton() button.setTitle("Click me", for: .normal)

// Add a target action to the button button.addTarget(self, action: #selector(buttonClicked(_:)), for: .touchUpInside)

// Define the action method @objc func buttonClicked(_ sender: UIButton) { if let buttonText = sender.title(for: .normal) { print("Button text: \(buttonText)") } }

In the above example, when the button is clicked, the buttonClicked() method is called and the text of the button is retrieved using the title(for:) method of the UIButton class. The text is then printed to the console.