Why delegate = nil?

Hello everybody!
I have a simple notebook app. In it app I have tableView in NotesTableViewController. Every cell its class, which haму two property:


var text: String? 
var name: String?

I want by click on cell transfer properties to EditorVC.

For this, I created the delegate:

protocol NotesTableViewContollerDelegate: class {
    func passItem(name: String?, text: String?)
}

In NotesTableViewController i created variable "delegate":

class NotesTableViewController: UITableViewController {
    //MARK: - Variables
    weak var delegate: NotesTableViewContollerDelegate?

This is my tableView(didSelectRowAt: ):

   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedItem = notes[indexPath.row] // Get selected item
        //Delegate
        delegate?.passItem(name: selectedItem.name, text: selectedItem.text)
        //Present EditorVC
        let editorVC = storyboard?.instantiateViewController(withIdentifier: "EditorViewController") as! EditorViewController
        present(editorVC,animated: true,completion: nil)
    }

And this is my EditorVC:

class EditorViewController: UIViewController {
    //MARK: - Variables & Outlets
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var textView: UITextView!
    
    
    //MARK: - Private Methods
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    //MARK: - Actions
    @IBAction func doneBarButtonClicked(_ sender: UIBarButtonItem) {
        dismiss(animated: true, completion: nil)
        //NotesTableViewControllerDelegate
        let notesVC = storyboard?.instantiateViewController(withIdentifier: "notesTableViewController") as! NotesTableViewController // Get NotesTableViewController
        notesVC.delegate = self
    }
    @IBAction func cancelButtonClicked(_ sender: UIBarButtonItem) {
        print(#function)
        // NotesTableViewControllerDelegate
        let notesVC = storyboard?.instantiateViewController(withIdentifier: "notesTableViewController") as! NotesTableViewController // Get NotesTableViewController
        notesVC.delegate = self
        
    }
    
}
//MARK: - NotesTableViewControllerDelegate
extension EditorViewController: NotesTableViewContollerDelegate {
    func passItem(name: String?, text: String?) {
        textView.text = text
        nameTextField.text = name
        
        print(#function)
    }
}

Accepted Reply

Where do you see delegate as nil ? Please be precise in your question.


Even if the overall logic is not totally clear (why do you reinstantiate notesVC ?), it seems you have 2 errors:

you redeclare notesVC, so it is a local constant that disappears as soon as you end the func.

let notesVC = storyboard?.instantiateViewController(withIdentifier: "notesTableViewController") as! NotesTableViewController // Get NotesTableViewController


Create a notesVC at class level, and remove the let to use this class property.

Replies

Where do you see delegate as nil ? Please be precise in your question.


Even if the overall logic is not totally clear (why do you reinstantiate notesVC ?), it seems you have 2 errors:

you redeclare notesVC, so it is a local constant that disappears as soon as you end the func.

let notesVC = storyboard?.instantiateViewController(withIdentifier: "notesTableViewController") as! NotesTableViewController // Get NotesTableViewController


Create a notesVC at class level, and remove the let to use this class property.