Reputation: 495
(Using Swift 2 and Xcode 7)
I have a UIAlertController that prompt the user the enter a name.
I'm having difficulties getting the value that the user enters, so any heads up would be great.
I have the following code;
func promptUserToEnterSessionName() -> String{
let sessionNameController = UIAlertController(title: "Save your yoga session", message: "Please enter session name", preferredStyle: .Alert)
let cancelAction = (UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
let saveAction = (UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: enteredSessionName))
sessionNameController.addTextFieldWithConfigurationHandler {
(textField) -> Void in
self.sessionName = textField
}
sessionNameController.addAction(cancelAction)
sessionNameController.addAction(saveAction)
self.presentViewController(sessionNameController, animated: true, completion: nil)
//savedSessionName = (sessionNameController.textFields![0] as UITextField).text!
//print(savedSessionName)
return ""
}
func enteredSessionName(alert: UIAlertAction!){
print("sessionName" + String(sessionName))
}
Upvotes: 0
Views: 126
Reputation: 2381
So another possible solution is to save the whole UITextField
in your member variable.
var textField: UITextField?
sessionNameController.addTextFieldWithConfigurationHandler {
(textField) -> Void in
self.textField = textField
}
func enteredSessionName(alert: UIAlertAction!){
print("sessionName" + String(self.textField.text))
}
Upvotes: 2