我有两个视图控制器模拟一个包含单元格的电子表格,用户在文本字段中输入,并且在两个场景中显示计算出的单元格。
计算由calc按钮触发,该按钮从文本字段获取用户输入,并创建单元格实例和任何计算。
VCs之间的导航由Segue处理,并且有发送任何数据的用户默认功能。
问题是第二个视图控制器单元格D5中的文本字段在第二次访问该VC时为空,而第一个视图控制器正常,始终显示信息。第二个VCL D5中的单元格对象是第一次创建并显示的,但在第二次访问时没有显示,但对象确实存在。

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])

}

//第二个表视图控制器
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
}

ios - 第二个ViewController返回时不显示信息-LMLPHP
ios - 第二个ViewController返回时不显示信息-LMLPHP
强文本

最佳答案

请用以下代码替换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)
}

但请记住,在返回FirstViewContoller之前,需要先单击calc,否则textfield中的值将不会持久。

关于ios - 第二个ViewController返回时不显示信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52860591/

10-14 21:57