Reload View from Modal View Controller with CoreData in Xcode

532 views Asked by At

I was following a YouTube tutorial on how to create a to-do list with CoreData and my app can build and run however instead of using another view controller to create a task, I created a modal view controller to be displayed over the regular view controller. The problem is it saves it to the CoreData but only displays when the app is reset, this is all the code used for the regular view controller where the tasks should appear:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableViewTest: UITableView!

var tasks : [Task] = []


override func viewDidLoad() {
    super.viewDidLoad()

    tableViewTest.dataSource = self
    tableViewTest.delegate = self
    self.navigationController?.isNavigationBarHidden = true

}

override func viewWillAppear(_ animated: Bool) {
    getData()

    tableViewTest.reloadData()
}

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

func tableView(_ tableViewTest: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    let task = tasks[indexPath.row]
    cell.textLabel?.text = task.name!
    return cell
}

func getData() {
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    do {
        tasks = try context.fetch(Task.fetchRequest())
    }
    catch {
        print("Fetch Error")
    }


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

and this is the code for the modal view controller where the user enter is information to be saved to CoreData:

class popVCAdd: UIViewController {

@IBOutlet weak var textField: UITextField!
@IBOutlet weak var popViewAc: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    popViewAc.layer.cornerRadius = 20
    popViewAc.layer.masksToBounds = true

    let toolbar = UIToolbar()
    toolbar.sizeToFit()
    textField.inputAccessoryView = toolbar
    let keyboardDone = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.disappearKey))
    toolbar.setItems([keyboardDone], animated: false)
}

@IBAction func doneBtn(_ sender: Any) {

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    let task = Task(context: context) 

    task.name = textField.text!

    (UIApplication.shared.delegate as! AppDelegate).saveContext()

}

@IBAction func dismissPop(_ sender: Any) {
    dismiss(animated: true, completion: nil) 
}

    func disappearKey() {
     view.endEditing(true)

    }

}

Does anybody know what's wrong with it?

1

There are 1 answers

0
Dheeraj D On BEST ANSWER

Please Change you ModalPresantaion Style to Full Screen

See Below Screen Shot:

Select Segue First:

enter image description here

Change Its Presantation Style to Full Screen:

enter image description here

I am Suggesting you above changes because:

viewWillAppear of your ViewController is not calling after Dismissing from your popVCAdd Controller.