【问题标题】:How to pass value from DetailView to TableViewList [duplicate]如何将值从 DetailView 传递到 TableViewList [重复]
【发布时间】:2017-09-18 05:59:13
【问题描述】:

我正在使用 Swift 3 构建一个简单的应用程序。所以我有一个 TableView 列表和一个详细信息视图。所以我创建了两个方法来将项目从详细视图添加到表视图列表。

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")
        }
    }

现在有什么方法可以将一些值作为布尔值传递给 TableViewList?

我想传递例如这个参数

布尔 isNew = true |假的

编辑 我不知道我是否使用了正确的方法。但我已将此变量插入到 Detail.swift 类中:

var isNew : Bool = true

在 TableView.swift 类中,我使用此代码读取此信息:

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()
            }

【问题讨论】:

  • 您可以使用委托将值从 DetailView 传递到 tableView 列表。
  • 你可以使用 NsnotificationCenter 来传值
  • @Karthick 如何从委托传递价值?

标签: ios swift uitableview xcode8


【解决方案1】:

有两种方法可以做到这一点。

  • 委托/协议
  • 通知中心

Delegate 非常适合将值从 Details 传递到 List,因为 delegate 用于 1 对 1 的消息传递,而 NotificationCenter 用于广播。

在这里你可以得到它的例子。 Pass data back to previous viewcontroller

【讨论】:

  • 我刚刚编辑了我的问题,你能看到吗?
猜你喜欢
  • 2023-04-09
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 2016-07-22
  • 2012-05-22
  • 1970-01-01
相关资源
最近更新 更多