如何從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);
}