我正在尝试显示UIAlertController
带有的UITextView
。当我添加行时:
//Add text field
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
}
我收到一个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
。
我已经从这里下载了代码,并且工作正常。我将该方法复制并粘贴到我的代码中,但它中断了。最后一行的自我存在没有影响。
UITextField
,而不是UITextView
?