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 January 2026

1 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
$35.99 $44.99
Save 20%
Mastering Swift 6: Modern programming techniques for high-performance apps in Swift 6.2
2 Modern Swift Programming: From Fundamentals to Building Your First Apple Apps

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

BUY & SAVE
$24.99
Modern Swift Programming: From Fundamentals to Building Your First Apple Apps
3 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
$26.70 $44.99
Save 41%
iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success
4 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
$35.99 $44.99
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
5 Swift Programming: A Detailed Guide to Learning Essential Concepts and Mastering Complex Techniques

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

BUY & SAVE
$24.94
Swift Programming: A Detailed Guide to Learning Essential Concepts and Mastering Complex Techniques
6 Java for Beginners: Build Your Dream Tech Career with Engaging Lessons and Projects

Java for Beginners: Build Your Dream Tech Career with Engaging Lessons and Projects

BUY & SAVE
$14.41
Java for Beginners: Build Your Dream Tech Career with Engaging Lessons and Projects
7 Coding iPhone Apps for Kids: A Playful Introduction to Swift

Coding iPhone Apps for Kids: A Playful Introduction to Swift

BUY & SAVE
$18.95 $29.95
Save 37%
Coding iPhone Apps for Kids: A Playful Introduction to Swift
8 Learning Swift: Building Apps for macOS, iOS, and Beyond

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

BUY & SAVE
$26.74 $49.99
Save 47%
Learning Swift: Building Apps for macOS, iOS, and Beyond
9 Head First Swift: A Learner's Guide to Programming with Swift

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

  • BOOST EFFICIENCY: STREAMLINES PROCESSES FOR INCREASED PRODUCTIVITY.

  • USER-FRIENDLY DESIGN: EASY TO USE, REDUCING TRAINING TIME AND COSTS.

  • HIGH-QUALITY RESULTS: CONSISTENT PERFORMANCE FOR CUSTOMER SATISFACTION.

BUY & SAVE
$38.93 $79.99
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.