3.3.2. Send data back from Screen 2 to Screen 1: delegating to ViewController

Now, let's enable the action for buttonSendMood in ShowViewController.swift file. So let's update viewDidLoad() function and add a new selector method onSendButtonTapped():

// setting up buttonSendMood...
override func viewDidLoad() {
        super.viewDidLoad()
        //setting the view background to white...
        view.backgroundColor = .white
        
        // MARK: initializing the UI elements...
        setupLabelMessage()
        setupLabelMoodInstructions()
        setupMoodPicker()
        setupButtonSendMood()
        
        // MARK: setting up constraints...
        initConstraints()
        
        //MARK: add a delegate to on 
        //button send mode tapped...
        buttonSendMood.addTarget(self, action: #selector(onSendMoodButtonTapped), for: .touchUpInside)
    }
// MARK: on send button tapped...
@objc func onSendMoodButtonTapped(){
    
}

Declaring the delegate variable in ShowViewController.swift

Before we write something in onSendMoodButtonTapped(), we need to do a few more things. First, if we want to send the mood back to the first screen (ViewController), we need to ask the ViewController to receive the data and do the tasks afterward. We are delegating the tasks to VIewController after we click on buttonSendMood. So, we create a variable delegate in ShowViewController.swift file that can hold the reference to the instance of ViewController:

Initializing delegate to the instance of ViewController (self)

Now, let's go back to ViewController class and ensure we set the delegatevariable's value before pushing ShowViewController into the NavigationController. By doing that, we are ensuring that the instance of ShowViewController can have access to the instance of ViewController. So let's update the @objc func onButtonSendTapped() in ViewController:

Doing delegated tasks in ViewController

Now, we need to add a method in ViewController to conduct the delegated tasks from ShowViewController:

Here, delegateButtonSendMood(mood:String) receives a String (mood) as a parameter. Let's just print the mood for now.

Now, let's switch back to ShowViewController. We now need to call this delegateButtonSendMood(mood:String) method when the user taps on onSendMoodButton. We can write:

Since the instance of ShowViewController gets populated with the variable delegate already set to the instance of ViewController, we can call delegateButtonSendMood() method from ShowViewController. We are calling the method with selectedMood as the parameter. Now, let's run the app and check if ViewController can print the mood.

We are yet to do another task. We need to pop the ShowViewController after we click on buttonSendMood. So we will add navigationController?.popViewController(animated: true) to @objc func onSendMoodButtonTapped().

Now let's run it again:

Yay! We now learned how to send data back and forth between two screens!

Now, your task is to show the user's mood in labelMood at ViewController screen.

Next, we will discuss displaying an image related to the user's mood.

Last updated

Was this helpful?