如何将TextField添加到Swift中的UIAlertController

我想显示一个UITextView的UIAlertController。 当我添加该行时:

//Add text field alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in } 

我得到一个运行时错误:

致命错误:意外地发现零,而解包一个可选的值

  let alertController: UIAlertController = UIAlertController(title: "Find image", message: "Search for image", preferredStyle: .Alert) //cancel button let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in //cancel code } alertController.addAction(cancelAction) //Create an optional action let nextAction: UIAlertAction = UIAlertAction(title: "Search", style: .Default) { action -> Void in let text = (alertController.textFields?.first as! UITextField).text println("You entered \(text)") } alertController.addAction(nextAction) //Add text field alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in textField.textColor = UIColor.greenColor() } //Present the AlertController presentViewController(alertController, animated: true, completion: nil) 

这是通过一个IBAction在我的ViewController中呈现的。

我已经从这里下载了代码: http : //sourcefreeze.com/uialertcontroller-ios-8-using-swift/它工作正常。 我复制并将该方法粘贴到我的代码,并打破。 最后一行的自我存在没有影响。

使用这段代码,我成功地在我的应用程序中运行这段代码。

  @IBAction func addButtonClicked(sender : AnyObject){ let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: UIAlertControllerStyle.Alert) let saveAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { alert -> Void in let firstTextField = alertController.textFields![0] as UITextField let secondTextField = alertController.textFields![1] as UITextField }) let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action : UIAlertAction!) -> Void in }) alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in textField.placeholder = "Enter First Name" } alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in textField.placeholder = "Enter Second Name" } alertController.addAction(saveAction) alertController.addAction(cancelAction) self.presentViewController(alertController, animated: true, completion: nil) } 

编辑:Swift 3.0版本

 @IBAction func addButtonClicked(_ sender: UIButton){ let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: .alert) let saveAction = UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in let firstTextField = alertController.textFields![0] as UITextField let secondTextField = alertController.textFields![1] as UITextField print("firstName \(firstTextField.text), secondName \(secondTextField.text)") }) let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: { (action : UIAlertAction!) -> Void in }) alertController.addTextField { (textField : UITextField!) -> Void in textField.placeholder = "Enter First Name" } alertController.addTextField { (textField : UITextField!) -> Void in textField.placeholder = "Enter Second Name" } alertController.addAction(saveAction) alertController.addAction(cancelAction) self.present(alertController, animated: true, completion: nil) } 

在Swift 3.0中添加一个文本字段:

  let alertController = UIAlertController(title: "Title", message: "", preferredStyle: .alert) alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in let textField = alertController.textFields![0] as UITextField // do something with textField })) alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in textField.placeholder = "Search" }) self.present(alertController, animated: true, completion: nil) 

所以我开始检查,看看我的代码中可能有不同的代码。 我注意到我的ViewController扩展

 UITextFieldDelegate 

这显然意味着我需要设置任何子UITextView的委托:

 alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in searchTextField = textField searchTextField?.delegate = self //REQUIRED searchTextField?.placeholder = "Enter your search terms" } 

很好的回答稍作修改,以显示如何使用文本字段:

 func addRow (row: Int, bodin: String, flag: Int) { let alertController = UIAlertController(title: bodin, message: "", preferredStyle: .alert) alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in _ = alertController.textFields![0] as UITextField })) alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in switch flag { case 0: textField.keyboardType = UIKeyboardType.phonePad textField.placeholder = "Enter Number" case 1: textField.keyboardType = UIKeyboardType.emailAddress textField.placeholder = "Enter Email" default: break } })