如何从swift中的警报框中检索输入?我不明白为什么我的代码无法正常工作。我是一名C++程序员,所以我对swift很陌生。出于某种原因,当我到达我的印刷线时,它只是说:“添加的新样式是:”这就是全部。它不会打印出哪些用户在文本框中因故类型。这里是我的代码如何从Swift中的警报框中检索输入?
// Generate a text field for user input
func generateTextField()
{
//1. Create the alert controller.
var tempStyle = "";
var alert = UIAlertController(title: "Add a New Style", message: "Enter the name of the new hairstyle below", preferredStyle: .Alert);
//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.placeholder = "Your New Hairstyle Goes Here..";
})
//3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
let textField = alert.textFields![0] as UITextField
tempStyle = textField.text!;
}))
// 4. Present the alert.
self.presentViewController(alert, animated: true, completion: nil)
print("New Style Added is: " + tempStyle);
}