I have a problem with my Swift app. I used Swift 3 and Xcode 8.
I have implemented a TableViewController and DetailViewController.
So I want to add a new item and then refresh automatically the TableViewController.
So This is the code of TableViewController that I called when I click on OK button from DetailViewController.
@IBAction func tornaAllaLista(_ segue: UIStoryboardSegue){
do {
var vistaDettaglio: AggiungiLuceViewController = segue.source as! AggiungiLuceViewController
if(vistaDettaglio.nuovaLuce != nil){
//verifico se devo aggiungere un valore o lo devo aggiornare
print(vistaDettaglio.isNew)
if(vistaDettaglio.isNew){
self.listaLuci.append(vistaDettaglio.nuovaLuce!)
}else{
}
self.tabella.reloadData()
}
} catch let errore {
print("[CDC] problema tornaAllaLista")
print(" Stampo l'errore: \n \(errore) \n")
}
}
If the vistaDettaglio.nuovaLuce is not null I want to add this new Items in my TableView.
This is the code of DetailViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//se il pulsante cliccato è diverso da OK torno indietro
if sender as? NSObject != self.buttonOK{
return
}
let nomeLuce = self.textNomeLuce.text!
let pinArduino = Int16(self.textPinArduino.text!)
let tipoLuce = self.textTipoLuce.text!
self.nuovaLuce?.descrizione = nomeLuce
self.nuovaLuce?.pin_arduino = pinArduino!
self.nuovaLuce?.tipo_luce = tipoLuce
//DEVO VERIFICARE SE SONO IN MODIFICA O SALVATAGGIO
if(self.nuovaLuce != nil && (self.nuovaLuce?.id)! > 0){
self.isNew = false;
LuciKitCoreDataController.shared.update(updateLuci: self.nuovaLuce!)
}else if(nomeLuce.characters.count>0){
self.isNew = true
//ho inserito almeno un carattere
let idInsert = LuciKitCoreDataController.shared.addLuce(descrizione: nomeLuce, pin_arduino: Int(pinArduino!), id: -1 , tipoLuce: tipoLuce)
self.nuovaLuce?.descrizione = nomeLuce
self.nuovaLuce?.pin_arduino = pinArduino!
self.nuovaLuce?.tipo_luce = tipoLuce
self.nuovaLuce?.id = idInsert
}else{
let alert = UIAlertController(title:"Attenzione", message: "Inserire un nome per la Luce", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated:true, completion: nil)
}
}
So if I try to add a new Items, I have vistaDettaglio.nuovaLuce = NIL.
How can I fixed this problem?