如何找到线?意外地发现零
问题描述:
当我尝试运行我的应用程序,我得到的错误,指出:如何找到线?意外地发现零
fatal error: unexpectedly found nil while unwrapping an Optional value.
可有人如果告诉我它的方式来找到问题的所在行? 不幸的是,我没有看到模拟器崩溃的红线。
我粘贴了所有的代码,但问题必须与警报功能有关,因为它工作正常,直到我试图实现它。
import UIKit
var list = ["Visa code: 1234", "Mastercard code: 4321"]
class notesVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var messageLabel: UILabel!
var userMessage = "Sample text"
var theUserText: UITextField?
@IBOutlet weak var tabelView: UITableView!
@IBAction func addItemButton(_ sender: Any) {
let alertController = UIAlertController(title:"title",
message: "message",
preferredStyle: .alert)
alertController.addTextField(configurationHandler: theUserTextFunc)
let okAction = UIAlertAction(title: "OK",
style: .default,
handler: self.okHandler)
let cancleAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancleAction)
self.present(alertController, animated: true)
}
func theUserTextFunc(textField: UITextField){
theUserText = textField
}
func okHandler(alert: UIAlertAction!){
list.append((theUserText?.text)!)
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return (list.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return(cell)
}
// Swipe to delete an item
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete{
list.remove(at: indexPath.row)
tabelView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
messageLabel.text = userMessage
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func customInit(userMessage: String) {
self.userMessage = userMessage
}
}
答
我想你的代码。它的工作原理完美无瑕,只是你错过了重新加载OkHandler的表格。
所以我怀疑问题会与你的IBOutlet或IBAction连接。检查周围...
import UIKit
var list = ["Visa code: 1234", "Mastercard code: 4321"]
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var messageLabel: UILabel!
var userMessage = "Sample text"
var theUserText: UITextField?
@IBOutlet weak var tabelView: UITableView!
@IBAction func addItemButton(_ sender: Any) {
let alertController = UIAlertController(title:"title",
message: "message",
preferredStyle: .alert)
alertController.addTextField(configurationHandler: theUserTextFunc)
let okAction = UIAlertAction(title: "OK",
style: .default,
handler: self.okHandler)
let cancleAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancleAction)
self.present(alertController, animated: true)
}
func theUserTextFunc(textField: UITextField){
theUserText = textField
}
func okHandler(alert: UIAlertAction!){
list.append((theUserText?.text)!)
self.tabelView.reloadData()
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return (list.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return(cell)
}
// Swipe to delete an item
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete{
list.remove(at: indexPath.row)
tabelView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
messageLabel.text = userMessage
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func customInit(userMessage: String) {
self.userMessage = userMessage
}
}
+0
谢谢。问题是与.xib文件的连接。 –
答
试着改变你的插件代码
alertController.addTextField(configurationHandler: theUserTextFunc)
到 -
alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
textField.placeholder = "Search"
// Set other textfield values that you want
})
是当你按下OK或只要你执行它发生? – GIJOW
尝试创建[异常断点](https://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode)并重现崩溃;你可能会得到你的代码崩溃的地方。 – the4kman
它可能会发生,因为var theUserText:UITextField?以及你如何使用它。大多数时候你按OK,但我可能是错的 –