将表中的数据从View保留到另一个

时间:2017-11-20 00:38:01

标签: ios swift3

我正在尝试将值从一个视图发送到另一个视图,并将它们作为数组打印在表中。该程序工作并显示数据,但问题是当我尝试向表中添加另一个值时,当我返回到具有该表的视图时,先前的值不再存在。

Here is the story board the view of the left have the tables to display the data and the one of the right is where I add the data

在这段代码中,我将数据发送到另一个视图

XmlNode datanode = doc.DocumentElement.SelectSingleNode("/project/data");
XmlNode innerDataNode;
foreach (XmlNode dataVar in datanode)
{
    innerDataNode = datanode.SelectSingleNode("/subdata");
    int subdataVal = XmlConvert.ToInt16(innerDataNode.InnerText);
    //(...)
}

在这段代码中是显示数据的表视图

import UIKit



class NewContactoViewController: UIViewController, UITextFieldDelegate {

var contacto: String = ""
var numero: String = ""

@IBOutlet weak var contactoField: UITextField!
@IBOutlet weak var numField: UITextField!

let defaultValues = UserDefaults.standard

@IBAction func addButton(_ sender: UIButton) {
    contacto = contactoField.text!
    numero = numField.text!

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    var secondController = segue.destination as! ContactosViewController

    secondController.contactos = contactoField.text!
    secondController.numerosmov = numField.text!

}

override func viewDidLoad() {
    super.viewDidLoad()
    self.contactoField.delegate = self
    self.numField.delegate = self

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

3 个答案:

答案 0 :(得分:1)

这里有几个问题,我建议再次阅读有关tableView的内容(特别是“加载初始数据”部分) -

https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html

您的表正在从“tablacontacto”和“tablanumero”数组中获取数据。 您发送的代码中没有位置来填充这些数组。 (当这些表在屏幕上时,你看到了什么吗?)

Plus您只使用“func datosRecividos(_ contactosr:String,_ numerosr:String)”更新这些数组 - 此方法仅在viewDidLoad中调用一次,并且在您切换回此屏幕时不会调用此方法

另外,您无法从userDefaults向阵列中获取数据,也无法将“NewContactoViewController”中的“新”数据保存到userDefaults。

答案 1 :(得分:0)

将这两个变量设为静态变量

internal static var CONTACTTO: String = ""
internal static var NUMERO: String = ""

然后使用

访问此变量
NewContactoViewController.CONTACTTO
NewContactoViewController.NUMERO

然后不需要使用

发送此值
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        var secondController = segue.destination as! ContactosViewController

        secondController.contactos = contactoField.text!
        secondController.numerosmov = numField.text!

}

或者您可以将这些值保存在共享首选项中

答案 2 :(得分:0)

您可以在NewContactoViewController

中声明更改数据的委托
import UIKit

//Create a delegate for data changing
protocol ContactChageDelegate: class {
    func contactChanged(newContact: String, newNumber: String)
}

class NewContactoViewController: UIViewController, UITextFieldDelegate {
//declare delegate variable
weak var contactChageDelegate: ContactChageDelegate?
...



@IBAction func addButton(_ sender: UIButton) {
    contacto = contactoField.text!
    numero = numField.text!

    //if need notify data changing. maybe it will not change
    //if data change {
        self.contactChageDelegate.contactChanged(newContact: contacto, newNumber: numero)
    //}
}

并在ContactosViewController中使用此委托,如下所示:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    //let self to delegate
    if let newContactoViewController = segue.destination as? NewContactoViewController {
        newContactoViewController.contactChageDelegate = self
    }
}

...

//implement delegate method
extension ContactosViewController: ContactChageDelegate {
    internal func contactChanged(newContact: String, newNumber: String) {
          //now you have new values
          //change your data array
          //and reload table
    }
}