如何定位线?意外发现没有

时间:2017-08-02 18:20:20

标签: ios xcode crash

当我尝试运行我的应用时,我收到错误说明:

  

致命错误:在打开Optional时意外发现nil   值。

有人能告诉我它是否有办法找到问题所在的那一行? 不幸的是,我没有得到模拟器崩溃的红线。

我粘贴了所有代码,但问题必须与alert函数有关,因为它在我尝试实现之前一直正常工作。

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
    }
}

2 个答案:

答案 0 :(得分:0)

尝试更改添加代码

alertController.addTextField(configurationHandler: theUserTextFunc)

到 -

alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
    textField.placeholder = "Search"
    // Set other textfield values that you want
})

答案 1 :(得分:0)

我尝试了你的代码。除了你错过了在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
}

}