我想将两个不同的数据从两个不同的视图控制器(Table1VC-Table2VC)传递给一个扩展(MainVC)。

我做到了,它的工作..但问题是:

当我传递第一个数据时,这里没有问题
但是问题是当我传递第二个数据时,第一个数据消失了!

这是我到目前为止所做的:

表1VC:

import UIKit

class Table1ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource{

@IBOutlet weak var tableview: UITableView!
var city = ["data1" , "data2" , "Data3"]

var passit = ""

override func viewDidLoad() {
    super.viewDidLoad()

    tableview.delegate = self
    tableview.dataSource = self

    self.navigationController?.navigationBar.tintColor = UIColor.orange

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return city.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()

    cell.textLabel?.text = city[indexPath.row]

    return cell
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    passit = city[indexPath.row]
    performSegue(withIdentifier: "showtable1", sender: city[indexPath.row])
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    let passData1 = segue.destination as! MainVC
        passData1.passcity1 = passit

    }



}


表2VC

import UIKit

class passVC: UIViewController , UITableViewDelegate , UITableViewDataSource{

@IBOutlet weak var tableView: UITableView!
   var city2 = ["data1" , "data2" , "Data3"]


var passit = ""

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    self.navigationController?.navigationBar.tintColor = UIColor.orange

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return city2.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()

    cell.textLabel?.text = city2[indexPath.row]

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    passit = city2[indexPath.row]

    performSegue(withIdentifier: "showtable2", sender: city2[indexPath.row])
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    let passData2 = segue.destination as! MainVC
    passData2.passcity2 = passit

}



}


主VC

import UIKit

class MainVC: UIViewController {

@IBOutlet weak var passText1: UITextField!
@IBOutlet weak var passText2: UITextField!



var passcity1 = ""
var passcity2 = ""

override func viewDidLoad() {
    super .viewDidLoad()



    passText1.text = passcity1
    passText2.text = passcity2


}

}


传递的数据将在MainVC的文本字段中

最佳答案

这有点像视图堆栈的基础知识和视图问题的生命周期。
当您键入该行时:

let passData1 = segue.destination as! MainVC

这意味着您要创建MainVC的一个实例,并要填充该对象的数据。

搜索到该控制器并返回到主控制器的那一刻,您丢失了具有第一个值的先前对象。

例:
从VC1:
let passData1 = segue.destination as! MainVC->表示告诉编译器给我一个对象名称“ passData1”,此后在passData1上执行的任何操作将仅影响passData1

从VC2:
let passData2 = segue.destination as! MainVC->表示告诉编译器给我一个对象名称“ passData2”,此后在passData2上执行的任何操作将仅影响passData2。

总而言之,您的对象已更改,因此该对象的先前状态会丢失。当您第一次离开MainVC时,即下次创建任何MainVC对象时,它都是MainVC的新副本。您将需要同时设置value1和value2。

如果要保留passData1的值,则将所需的值保存在某处,并在传递passData2时获取这些值并传递完整的集合。

关于ios - 将两个变量传递到同一 View Controller swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43969458/

10-11 19:58