如何将值从DetailView传递到TableViewList [英] How to pass value from DetailView to TableViewList
问题描述
我正在使用Swift 3构建一个简单的应用程序。所以我有一个TableView List和一个Detail View。所以我创建了tow方法,将细节视图中的项目添加到TableView列表。
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:
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
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传递一些值作为布尔值?
Now there is any way to pass at TableViewList some value as a Boolean value?
我想传递例如这个参数
布尔isNew = true | false
Boolean isNew = true | false
编辑
我不知道我是否使用了正确的方法。但我已将此变量插入Detail.swift类:
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
在TableView.swift类中,我使用此代码来读取此信息:
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()
}
推荐答案
有两种方法可以做到这一点。
There is 2 ways to do this.
- 代表/协议
- NotificationCenter
委托非常适合将值从详细信息传递到列表,因为委托用于1到1个消息传递,而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.
在这里你可以得到它的例子。
将数据传回上一个viewcontroller
Here you can get example of it. Pass data back to previous viewcontroller
这篇关于如何将值从DetailView传递到TableViewList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!