Home>

In the previous question, I asked about the Todo app

"Even if Todo task was deleted, it was not deleted after rebooting"
https://www.tutorialfor.com/go.php?id=143865#

There is still one problem, that is, the task added on the add screen is not added even if it returns to the top screen, and it will be displayed after rebooting.

I want to add a task as soon as I press the add button.

Please give me an answer.

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int)->Int {
        // return the number of cells
        return cell_array.count
    }

    func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath)->UITableViewCell {
        // Create a cell variable
        let cell: UITableViewCell = tableView.dequeueReusableCell (withIdentifier: "TodoCell", for: indexPath)
        // Store the contents of the cell registered in UD
        cell.textLabel! .text = cell_array [indexPath.row]
        // return the cell to display
        return cell
    }
    // Delete the task
    func tableView (_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        let index = indexPath.row
        cell_array.remove (at: index)
        // Save to UD
        UserDefaults.standard.set (cell_array, forKey: "TodoList")
        UserDefaults.standard.synchronize ()
        tableView.reloadData ()
    }
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Do any additional setup after loading the view, typically from a nib.
        // Receive contents stored in UD
        if UserDefaults.standard.object (forKey: "TodoList")! = nil {
            cell_array = UserDefaults.standard.object (forKey: "TodoList") as! [String]
        }
    }
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
        // Dispose of any resources that can be recreated.
    }


}
import UIKit
// array to store variables
var cell_array = [String] ()
class AddController: UIViewController {
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Do any additional setup after loading the view.
    }
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet weak var textField: UITextField!
    @IBAction func addButton (_ sender: Any) {
        // Add the entered text
        cell_array.append (textField.text!)
        // Delete the textField value
        textField.text = ""
        // Register to UD
        UserDefaults.standard.set (cell_array, forKey: "TodoList")
        UserDefaults.standard.synchronize ()
    }

}

"Appendix"
1. Is this all right?

override func viewDidLoad () {
        super.viewDidLoad ()
        // Do any additional setup after loading the view, typically from a nib.
// // Receive contents stored in UD
// if UserDefaults.standard.object (forKey: "TodoList")! = nil {
// cell_array = UserDefaults.standard.object (forKey: "TodoList") as! [String]
//}
    }
    override func viewWillAppear (_ animated: Bool) {
        // Receive contents stored in UD
        if UserDefaults.standard.object (forKey: "TodoList")! = nil {
            cell_array = UserDefaults.standard.object (forKey: "TodoList") as! [String]
        }
       tableView.reloadDate () ← An error will occur. // Ambiguous reference to member 'tableView (_: numberOfRowsInSection :)'
    }