class x: UIViewController {
let fromLocationLbl = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
let editTextField = UITextField()
let alertController = UIAlertController(title: "Alert!", message: "Please enter from location", preferredStyle: .alert)
alertController.addTextField { editTextField in
editTextField.placeholder = "Enter correct name"
// editTextField.text = self.fromLocationLbl.text
}
let confirmAction = UIAlertAction(title: "Change", style: UIAlertActionStyle.default) { (UIAlertAction) in
print(editTextField.text) ///////printing optional("")
self.fromLocationLbl.text = editTextField.text
}
alertController.addAction(confirmAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
}
Asked
Active
Viewed 59 times
-3

Robert Dresler
- 10,580
- 2
- 22
- 40

Ravi
- 165
- 3
- 11
-
And what is the question? – mag_zbc Feb 07 '19 at 08:59
-
i want to assign text field test to label, but it is returning empty string – Ravi Feb 07 '19 at 09:03
1 Answers
0
editTextField
isn't the same UITextField
as you're adding to alert. In addTextField
closure, editTextField
is just name for parameter of closure which can be replaced with any other name
alertController.addTextField { textField in
textField.placeholder = "Enter correct name"
// textField.text = self.fromLocationLbl.text
}
You need to get reference for first text field in your alertController
let confirmAction = UIAlertAction(title: "Change", style: .default) { _ in
self.fromLocationLbl.text = alertController.textFields?.first?.text
}

Robert Dresler
- 10,580
- 2
- 22
- 40