Data not passing from one view controller to the other

Hi, I am in the process of building a to-do app and am getting stuck. My first view controller displays a table view with tasks in each cell, and has an accessory button. When the button is clicked, a new view controller is displayed, with again a table view. The add button (a navigation bar item) once again opens a new view controller where the user can add in a task name, with an add button at the bottom. When the button is clicked the following code runs:

@IBAction func addAction(_ sender: Any) {
    if taskNameOutlet.text != "" {
           delegate?.addSubTask(nameOfSubTask: taskNameOutlet.text!)
           navigationController?.popViewController(animated: true)

I use the protocol, which my view controller conforms to:

protocol AddSubTask {
//Changed the following two lines
func addSubTask(nameOfSubTask: String)

I then have the following code in the view controller:

func addSubTask(nameOfSubTask: String) {

I want to add more in that chunk of code (e.g. tasks[indexPath].miniTaskArr.append(miniTask(name2: nameOfSubTask))), yet the string “test” isn’t even being printed to my console.

How do I fix this? I just want the add button to delegate the view controller to call the func addSubTask so I can append values into an array. Thanks, Annique

Your viewcontroller layout seems complex. The first TVC lists the tasks. The third VC is an input screen for adding tasks. What does the second TVC list?

It is very complex, and probably more complex than needed (I am very new to coding haha). So the first TVC displays the tasks (from a array called tasks, which are of type Task, correct. The third VC is an input screen, correct. The second TVC is for displaying the array of mini-tasks (each thing of type Task, which is displayed on TVC 1, has an array of mini tasks).

It would be better to add your tasks, including subtasks, from your initial screen. You could place a bar button at the top left, with + symbol, and then have your input screen modally present itself. From that input screen the user would enter their task, and any sub-tasks, without having to navigate through to the third screen.

That is very smart. I will try that, thank you!

I’m a little bit confused about your issue with the action outlet not running. I’d have to look at your code itself I think and see how it relates to each viewcontroller. Try commenting all the code in your ibaction outlet and then have it call simple test function. Something like

func test() { print(“test”) }

and see if it gets called.

I could send you my code if you want. What’s happening is basically that the IBaction works, but the delegate doesn’t. I followed your suggestion, putting a print statement in both the the IBAction of the button and the function that gets called because of a delegate, yet only the first one prints.