0

When I try to run my app, I get the error stating:

fatal error: unexpectedly found nil while unwrapping an Optional value.

Can anybody tell me if its a way to locate the line where the problem is? Unfortunately, I don't get a red line where the simulator crashes.

I pasted in all of the code, but the problem must have to do with the alert function because it worked fine until I tried to implement that.

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
    }
}
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
Paal Aune
  • 353
  • 5
  • 10
  • is it happening when you press ok or as soon as you execute ? – GIJOW Aug 02 '17 at 18:23
  • Try creating an [exception breakpoint](https://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode) and reproduce the crash; you will likely get where your code crashes. – Tamás Sengel Aug 02 '17 at 18:27
  • it might be happening because of var theUserText: UITextField? and how you are using it. Mostly when you press ok but i might be wrong – Kapil G Aug 02 '17 at 18:27
  • It crashes when I execute the app. The theUserText textfield is from a .xib file with a textfield. Is it any other way to do this? – Paal Aune Aug 02 '17 at 18:29
  • change your theUserText type from optional. Do var theUserText: UITextField! and see if it gives any error – Kapil G Aug 02 '17 at 18:35
  • Thanks for the suggestion and the quick reply, but it did not change anything. – Paal Aune Aug 02 '17 at 18:40
  • Ok so how are you connecting your xib textfield with your theUserText field? – Kapil G Aug 02 '17 at 18:41
  • Hey have you registered your tableview with your cell.? – Dhruv Khatri Aug 02 '17 at 20:44

2 Answers2

0

Try changing your add code

alertController.addTextField(configurationHandler: theUserTextFunc)

to -

alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
    textField.placeholder = "Search"
    // Set other textfield values that you want
})
Kapil G
  • 4,081
  • 2
  • 20
  • 32
0

I tried your code. It works flawless except that you missed to reload on table on OkHandler.

So I suspect issue would be with ur IBOutlet or IBAction connections. Check around that...

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
}

}

Ethan Humphries
  • 1,786
  • 3
  • 19
  • 28
Chaitra
  • 16
  • 4