How to pass value from DetailView to TableViewList

2019-02-21 06:15发布

This question already has an answer here:

I'm building a simple app with Swift 3. So I have a TableView List and a Detail View. So I have created, tow method to add items from Detail View to TableView List.

Detail.swift:

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!
        //DEVO VERIFICARE SE SONO IN MODIFICA O SALVATAGGIO
        if((self.nuovaLuce?.id)! > 0){
            self.nuovaLuce?.descrizione = nomeLuce
            self.nuovaLuce?.pin_arduino = pinArduino!
            LuciKitCoreDataController.shared.update(updateLuci: self.nuovaLuce!)
        }else if(nomeLuce.characters.count>0){
            //ho inserito almeno un carattere
            let idInsert = LuciKitCoreDataController.shared.addLuce(descrizione: nomeLuce, pin_arduino: Int(pinArduino!), id: (self.nuovaLuce?.id)!)
            self.nuovaLuce?.descrizione = nomeLuce
            self.nuovaLuce?.pin_arduino = pinArduino!
            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)
        }

    }

TableView.swift

@IBAction func tornaAllaLista(_ segue: UIStoryboardSegue){
        do {
            var vistaDettaglio: AggiungiLuceViewController = segue.source as! AggiungiLuceViewController
            if(vistaDettaglio.nuovaLuce != nil){
                self.listaLuci.append(vistaDettaglio.nuovaLuce!)
                self.tabella.reloadData()
            }else{

            }
        } catch let errore {
            print("[CDC] problema tornaAllaLista")
            print("  Stampo l'errore: \n \(errore) \n")
        }
    }

Now there is any way to pass at TableViewList some value as a Boolean value?

I want to pass for example this parameter

Boolean isNew = true | false

EDIT I don't know if I have used a correct way. But I have insert this variables into Detail.swift class:

var isNew : Bool = true

In TableView.swift class I have used this code to read this information:

var vistaDettaglio: AggiungiLuceViewController = segue.source as! AggiungiLuceViewController


    if(vistaDettaglio.nuovaLuce != nil){
                //verifico se devo aggiungere un valore o lo devo aggiornare
                if(vistaDettaglio.isNew){
                    self.listaLuci.append(vistaDettaglio.nuovaLuce!)
                }else{

                }
                self.tabella.reloadData()
            }

1条回答
可以哭但决不认输i
2楼-- · 2019-02-21 07:05

There is 2 ways to do this.

  • Delegate/Protocol
  • NotificationCenter

Delegate is perfect to pass value from Details to List, because delegate is used to 1 to 1 message passing, and NotificationCenter is used for broadcasting.

Here you can get example of it. Pass data back to previous viewcontroller

查看更多
登录 后发表回答