我正在尝试显示UIAlertController带有的UITextView。当我添加行时:
UIAlertController
UITextView
//Add text field alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in }
我收到一个Runtime错误:
Runtime
致命错误:解开Optional值时意外发现nil
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)
这ViewController通过呈现在我的内部IBAction。
ViewController
IBAction
我已经从这里下载了代码,并且工作正常。我将该方法复制并粘贴到我的代码中,但它中断了。最后一行的 自我 存在没有影响。
因此,我开始检查看看我的代码与工作代码之间可能有什么不同。我注意到我的ViewController扩展了
UITextFieldDelegate
显然这意味着我需要设置任何子UITextView的委托:
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in searchTextField = textField searchTextField?.delegate = self //REQUIRED searchTextField?.placeholder = "Enter your search terms" }