【发布时间】:2015-11-02 12:46:28
【问题描述】:
我正在尝试用UITextView 显示UIAlertController。当我添加该行时:
//Add text field
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
}
我收到Runtime 错误:
致命错误:在展开可选值时意外发现 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)
这是通过IBAction 在我的ViewController 中呈现的。
我已经从here 下载了代码,它工作正常。我将该方法复制并粘贴到我的代码中,但它中断了。最后一行出现 self 没有影响。
【问题讨论】:
-
UITextField,而不是UITextView?
标签: ios swift xcode uitextfield uialertcontroller