Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将表中的数据从一个视图保存到另一个视图_Ios_Swift3 - Fatal编程技术网

Ios 将表中的数据从一个视图保存到另一个视图

Ios 将表中的数据从一个视图保存到另一个视图,ios,swift3,Ios,Swift3,我试图将值从一个视图发送到另一个视图,并将它们作为数组打印到表中。程序工作并显示数据,但问题是,当我尝试向表中添加另一个值时,当我返回到包含该表的视图时,以前的值不再存在 在这段代码中,我将数据发送到另一个视图 import UIKit class NewContactoViewController: UIViewController, UITextFieldDelegate { var contacto: String = "" var numero: String = "" @I

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

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

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.
}
}
在这段代码中是显示数据的表视图

import UIKit

class ContactosViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let defaultValues = UserDefaults.standard

var contactos: String = ""
var numerosmov: String = ""
var tablacontacto = [String] ()
var tablanumero = [String] ()

let cellIdentifier: String = "cell"
let cellIdentifier2: String = "cell2"


@IBOutlet weak var contactoTable: UITableView!
@IBOutlet weak var numTable: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil)
    navigationItem.leftBarButtonItem = backButton

    // Do any additional setup after loading the view.

    datosRecividos(contactos, numerosmov)
    contactoTable.delegate = self
    numTable.delegate = self
    contactoTable.dataSource = self
    numTable.dataSource = self
    contactoTable!.reloadData()
    numTable!.reloadData()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    contactoTable.reloadData()
    numTable.reloadData()
}

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

func datosRecividos(_ contactosr: String, _ numerosr: String)
{
tablacontacto.append(contactosr)
    tablanumero.append(numerosr)
    let usercontacto = defaultValues.array(forKey: "contactoTable")
    let usernumero = defaultValues.array(forKey: "numeroTable")

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if (tableView.tag == 1)
    {
    return(tablacontacto.count)
    }
    else if (tableView.tag == 2)
    {
    return(tablanumero.count)
    }
    return 0
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)

    if (tableView.tag == 1)
    {
    cell.textLabel?.text = tablacontacto[indexPath.row] as! String

    }
    else if (tableView.tag == 2)
    {
       cell.textLabel?.text = tablanumero[indexPath.row] as! String

    }

    return(cell)
}





}

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

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!

}

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

这里有几处错误,我建议您再次阅读有关tableView的内容(尤其是“加载初始数据”部分)-

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

另外,您只在“func datosRecividos(\ucontactosr:String,\unumerosr:String)”中更新这些数组- 此方法在viewDidLoad中只调用一次,当您切换回此屏幕时不会调用


此外,您没有地方可以从用户默认值向阵列获取数据,也没有地方可以将“NewContactoViewController”中的“新”数据保存到用户默认值。

您可以在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
    }
}

是的,当我运行项目时,我可以在表格中添加值,表格显示数据,问题是当我尝试添加更多数据时,以前的数据不再存在,只显示新的数据。@EdwinLima您能告诉我用于在表格中显示数据的代码吗:“是的,当我运行项目时,我可以在表格中添加值,表格显示数据”?