Swift Tutorial: How to Add a TextField to an Alert Dialog (UIAlertController)

Описание к видео Swift Tutorial: How to Add a TextField to an Alert Dialog (UIAlertController)

Can you help me to buy a coffee:
https://www.buymeacoffee.com/coffeepr...

In this Swift tutorial, you will learn how to add a UITextField to an alert dialog using UIAlertController. We'll walk through the step-by-step process of creating a custom alert with a text field, allowing users to input text interactively. Whether you're building iOS apps or exploring iOS development, mastering this technique will enhance your user experience and add functionality to your app. Join us to unlock this essential skill and level up your Swift programming!

Doc: https://docs.google.com/document/d/1_...

or code:

// Create the alert controller
let alertController = UIAlertController(title: "Enter your name", message: nil, preferredStyle: .alert)

// Add a text field to the alert controller
alertController.addTextField { textField in
textField.placeholder = "Enter your name here"
}

// Add an action to the alert controller
let saveAction = UIAlertAction(title: "Continue", style: .default) { action in
// Handle the action when the "Save" button is tapped
if let textField = alertController.textFields?.first {
let enteredText = textField.text ?? ""
// Process the entered text as needed

}
}

// Add the "Save" action to the alert controller
alertController.addAction(saveAction)

// Add a cancel action to the alert controller (optional)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)

// Present the alert controller
present(alertController, animated: true, completion: nil)

Комментарии

Информация по комментариям в разработке