0

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?

bircastri
  • 2,169
  • 13
  • 50
  • 119
  • what do this line : LuciKitCoreDataController.shared.update(updateLuci: self.nuovaLuce!) – KKRocks Apr 21 '17 at 11:45
  • You can create a `protocol` to send data back to the `tableVC` or fetch data back on 'viewWillAppear' event. For that you can create a class reference of `detailVC` and keep it alive. On `viewWillAppear` of `tableVC` check if `detailVC` object have memory or not. if it does, try to fetch data. – Blind Ninja Apr 21 '17 at 11:49
  • @KKRocks that line update into database the object – bircastri Apr 21 '17 at 11:51
  • which line update your tableview ? – KKRocks Apr 21 '17 at 11:52
  • There are many, many examples of doing what you want. Search for `pass data back from segue`. Read through a couple examples and you'll be on your way. – DonMag Apr 21 '17 at 12:55
  • Please don't repost questions. Improve your question by adding more information. – vadian Apr 21 '17 at 13:41

1 Answers1

0

Reactive Cocoa will work wonders for this exact task. Have a look into it.

If that seems slightly too technical, you could try using custom notifications and the Notification Centre, or you could try using KVO to determine as and when the items in the detail view change.

royalmurder
  • 148
  • 1
  • 10