第二个ViewController返回时不显示信息

时间:2018-10-17 17:32:40

标签: ios swift

我有两个View控制器模拟带有Cells的电子表格,TextFields中的用户输入,并且在两个场景中显示了计算出的Cells。

通过calc按钮触发计算,该按钮从“文本字段”获取用户输入,并创建单元格实例和所有计算。

VC之间的导航由Segue处理,并且有用于发送任何数据的UserDefaults函数。

问题是,当您第二次访问该VC时,第二视图控制器单元D5中的TextField为空,而“第一视图控制器”正常,则始终显示该信息。在SecondVc Cell D5中创建了Cell对象,第一次显示该对象,但是第二次访问时未显示,但是该对象确实存在。

class MainTableViewController: UITableViewController {


@IBOutlet weak var C1ValueTextField: UITextField!
@IBOutlet weak var C2ValueTextField: UITextField!
@IBOutlet weak var C3ValueTextField: UITextField!
@IBOutlet weak var C4ValueTextField: UITextField!
@IBOutlet weak var C5ValueTextField: UITextField!


@IBAction func calc(_ sender: Any) {

    // Input C1
    let C1 = Cell(sheet: "In", colRowName: "C1", cellName: "", value: Int(C1ValueTextField.text!)!)
    C1.printCell()

    // Input C2
    let C2 = Cell(sheet: "In", colRowName: "C2", cellName: "", value: Int(C2ValueTextField.text!)!)
    C2.printCell()

    // calc C4
    let C4 = Cell(sheet: "In", colRowName: "C4", cellName: "", value: C1.value + C2.value)
    C4ValueTextField.text = String(C4.value)
    C4.printCell()


    Cell.saveCellsUserDefaults(cells: [C1, C2, C4])

}

// SecondTableViewController

class SecondTableViewController: UITableViewController {

var cells = Cell.getCellsUserDefaults()



@IBOutlet weak var D1ValueTextField: UITextField!
@IBOutlet weak var D2ValueTextField: UITextField!
@IBOutlet weak var D3ValueTextField: UITextField!
@IBOutlet weak var D4ValueTextField: UITextField!
@IBOutlet weak var D5ValueTextField: UITextField!


@IBAction func calc(_ sender: Any) {

    // D5 Problem: info disappears from TextField when navigating across VCs

    let D5 = Cell(sheet: "In", colRowName: "D5", cellName: "", value: Int(D5ValueTextField.text!)!)
    D5.printCell()

    Cell.saveCellsUserDefaults(cells: [D5])
}

override func viewDidLoad() {
    super.viewDidLoad()


    let cells = Cell.getCellsUserDefaults()



    // calc D1
    let D1 = Cell(sheet: "In", colRowName: "D1", cellName: "", value: cells[0].value)
    D1ValueTextField.text = String(D1.value)
    D1.printCell()


    // calc D2
    let D2 = Cell(sheet: "In", colRowName: "D2", cellName: "", value: cells[1].value)
    D2ValueTextField.text = String(D2.value)
    D2.printCell()

    // calc D3
    let D3 = Cell(sheet: "In", colRowName: "D3", cellName: "", value: D1.value + D2.value)
    D3ValueTextField.text = String(D3.value)
    D3.printCell()

   Cell.saveCellsUserDefaults(cells: [D1, D2, D3])

    // Calc D4 = D2 + D3

    let D4 = Cell(sheet: "In", colRowName: "D4", cellName: "", value: D1.value + D2.value)
    D4ValueTextField.text = String(D4.value)
    D4.printCell()




}

//在“单元格”类中:

 static func saveCellsUserDefaults(cells: [Cell])  {
    let encoder = JSONEncoder()
    if let encoded = try? encoder.encode(cells) {
        let defaults = UserDefaults.standard
        defaults.set(encoded, forKey: "pack")
    }
}


 static func getCellsUserDefaults()-> [Cell] {
    var cells = [Cell]()
    let decoder = JSONDecoder()
    if let cellData = UserDefaults.standard.data(forKey: "pack") {
        if let cellX = try? decoder.decode([Cell].self, from: cellData) {
            cells = cellX
            return cellX
        }
    }
    return cells
}

MainViewController

SecondViewController

强文本

2 个答案:

答案 0 :(得分:1)

请使用以下代码替换您的viewDidLoad方法,

override func viewDidLoad() {
    super.viewDidLoad()
    // llamar a UD y sacar todos los packs por pantalla ???????
    var cells = Cell.getCellsUserDefaults(forKey: "pack")

    // calc D1
    let D1 = Cell(sheet: "In", colRowName: "D1", cellName: "", value: cells[0].value)
    D1ValueTextField.text = String(D1.value)
    //D1.printCell()

    // calc D2
    let D2 = Cell(sheet: "In", colRowName: "D2", cellName: "", value: cells[1].value)
    D2ValueTextField.text = String(D2.value)
    //D2.printCell()

    // calc D3 = D1 + D2
    let D3 = Cell(sheet: "In", colRowName: "D3", cellName: "", value: D1.value + D2.value)
    D3ValueTextField.text = String(D3.value)
    //D3.printCell()

    Cell.saveCellsUserDefaults(cells: [D1, D2, D3], forKey: "pack")

    var pack2Cells = Cell.getCellsUserDefaults(forKey: "pack2")
    let D5 = Cell(sheet: "In", colRowName: "D5", cellName: "", value: pack2Cells.isEmpty ? 0 : pack2Cells[0].value)
    D5ValueTextField.text = String(D5.value)
}

但是请记住,您需要先单击calc,然后再返回FirstViewContoller,否则文本字段中的值将不会持久。

答案 1 :(得分:0)

请尝试在SecondViewController的viewDidLoad中添加以下行,然后检查,

let D5 = Cell(sheet: "In", colRowName: "D5", cellName: "", value: cells[4].value)
D5ValueTextField.text = String(D5.value)