我在试着拿出UIAlertController
分和UITextView
分.当我添加该行时:
//Add text field
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
}
我得到了Runtime
个错误:
致命错误:展开可选值时意外发现空
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
在my ViewController
中呈现的.
我已经从here下载了代码,它工作得很好.我将该方法复制并粘贴到我的代码中,但它崩溃了.self在最后一条线路上的存在没有任何影响.