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 October 2025

1 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
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 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
$44.99 $49.99
Save 10%
Mastering Swift 6: Modern programming techniques for high-performance apps in Swift 6.2
4 Swift Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Swift Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

BUY & SAVE
$44.69
Swift Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)
5 Learning Swift: Building Apps for macOS, iOS, and Beyond

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

BUY & SAVE
$28.49 $49.99
Save 43%
Learning Swift: Building Apps for macOS, iOS, and Beyond
6 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
$39.99
Swift Cookbook: Proven recipes for developing robust iOS applications with Swift 5.9
7 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
8 Mastering iOS 18 Development: Take your iOS development experience to the next level with iOS, Xcode, Swift, and SwiftUI

Mastering iOS 18 Development: Take your iOS development experience to the next level with iOS, Xcode, Swift, and SwiftUI

BUY & SAVE
$35.90 $41.99
Save 15%
Mastering iOS 18 Development: Take your iOS development experience to the next level with iOS, Xcode, Swift, and SwiftUI
9 Swift in Depth

Swift in Depth

  • COMPREHENSIVE GUIDE FOR SWIFT BEGINNERS AND PROS ALIKE.
  • INCLUDES PRACTICAL EXAMPLES TO BOOST CODING SKILLS QUICKLY.
  • ACCESS TO EXCLUSIVE ONLINE RESOURCES AND COMMUNITY SUPPORT.
BUY & SAVE
$49.99
Swift in Depth
+
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.