2017-08-02 1 views
0

Lorsque je tente de lancer mon application, je reçois l'erreur indiquant:Comment localiser la ligne? de façon inattendue trouvé nulle

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

Quelqu'un peut-il me dire si sa façon de localiser la ligne où est le problème? Malheureusement, je ne reçois pas une ligne rouge où le simulateur se bloque.

J'ai collé dans tout le code, mais le problème doit avoir à voir avec la fonction d'alerte car cela a fonctionné correctement jusqu'à ce que j'essaye d'implémenter cela.

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

est qu'il se passe lorsque vous appuyez sur ok ou dès que vous exécutez? – GIJOW

+0

Essayez de créer un [point d'arrêt d'exception] (https://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode) et de reproduire le crash; vous obtiendrez probablement où votre code se bloque. – the4kman

+0

cela pourrait se produire à cause de var theUserText: UITextField? et comment vous l'utilisez. Surtout quand vous appuyez sur OK, mais je peux me tromper –

Répondre

0

J'ai essayé votre code. Cela fonctionne parfaitement, sauf que vous avez manqué de recharger sur la table OkHandler.

Donc je pense que le problème serait avec vos connexions IBOutlet ou IBAction. Vérifiez autour de cette ...

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

Merci. Le problème était la connexion avec le fichier .xib. –

0

Essayez de changer votre code ajouter

alertController.addTextField(configurationHandler: theUserTextFunc) 

à -

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