I am trying to show a UIAlertController with a UITextView. When I add the line:
//Add text field alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in } I get a Runtime error:
fatal error: unexpectedly found nil while unwrapping an Optional value
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) This is presented inside my ViewController via an IBAction.
I have downloaded the code from here and it works fine. I copied and pasted that method into my code and it breaks. The presence of self on the last line has no impact.
UITextField, rather thanUITextView?