0

I want to make static table view to pass data into coredata to add new item to my TableView, but i dont know how to connect "Done" button to make action, from another view controller (UIView).

I got this: image

I wanted to make it i one ViewController, but when i'm switching my Table View to static cells i got problem solved by this: click

So, now i got AddItemTableViewController class, and i want to run this code on my "Done" button, but i don't know how to connect that button from another view.

import UIKit

class AddItemTableViewController: UITableViewController {

    @IBOutlet weak var titleTextField: UITextField!


    @IBAction func doneButtonPressed(_ sender: Any) {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let task = ItemsList(context: context)
        task.title = titleTextField.text!

        // Save the data to coredata
        (UIApplication.shared.delegate as! AppDelegate).saveContext()

        let _ = navigationController?.popViewController(animated: true)
    }
}

Could you please advice, how to deal with it? thanks in advance!

rmitula
  • 47
  • 2
  • 6
  • 3
    You should use `delegate` approach. Just declare a protocol in `AddItemTableViewController` and a `delegate` property, set it in your ViewController and implement protocol's method in it – alexburtnik Feb 23 '17 at 15:10
  • Delegate approach is one way, using blocks can be easier in some cases: http://stackoverflow.com/a/19867352/985027 – Lizza Feb 23 '17 at 16:08

0 Answers0