4. Sending data From one Tab to Another

We will use Notification Center to send data between the tabs. If the user taps on the "Send Hello" button from the Red Screen, it will send all the other tabs a message, "Hello From Red Screen." The other screens will display the message on the labels.

We now have to write code in the view controllers to enable this feature. Let's open RedViewController.swift file and write the following code:

//
//  RedViewController.swift
//  TabControllerDemo
//
//  Created by Sakib Miazi on 6/6/23.
//

import UIKit

class RedViewController: UIViewController {
    let redView = RedView()
    
    let notificationCenter = NotificationCenter.default
    
    override func loadView() {
        view = redView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        title = "Red"
        
        //MARK: setting observers...
        observeBlue()
        observeGreen()

        //MARK: send hello button...
        redView.buttonSend.addTarget(self, action: #selector(onButtonSendTapped), for: .touchUpInside)
    }
    
    //MARK: observing blue...
    func observeBlue(){
        notificationCenter.addObserver(
            self,
            selector: #selector(notificationReceived(notification:)),
            name: .fromBlue, object: nil
        )
    }
    
    //MARK: observing green...
    func observeGreen(){
        notificationCenter.addObserver(
            self,
            selector: #selector(notificationReceived(notification:)),
            name: .fromGreen, object: nil
        )
    }
    
    //MARK: handling notifications...
    @objc func notificationReceived(notification: Notification){
        redView.labelReceived.text = notification.object as! String
    }
    
    //MARK: sending hello to other screens...
    @objc func onButtonSendTapped(){
        notificationCenter.post(
            name: .fromRed,
            object: "Hello from Red!"
        )
    }
}

In the above code:

  • On line 13, we define the notification center.

  • We create a separate file NotificationNames.swift to set the names of the notifications.

    • static let fromRed = Notification.Name("fromRed")

    • static let fromGreen = Notification.Name("fromGreen")

    • static let fromBlue = Notification.Name("fromBlue")

  • Since this is the red screen, we will observe the notifications from the blue and green screens.

    • On lines 25 and 26, we call two methods (observeBlue() and observeGreen()) to set up the observers.

    • observeBlue() method observes the notifications from the Blue Screen. When a new notification from the Blue Screen is received, we handle the notification in notificationReceived(notification: Notification) method.

      • On lines 51 through 53, we handle the notification. In the method, we set the label's text to the notification's text.

  • On line 29, we add the target for buttonSend.

    • On tapping buttonSend we post the notification with the string: "Hello from Red!" (lines 56 through 61).

We write the other two view controllers regarding the Blue and Green screens similarly.

Now, if we run the app now, we will see:

Last updated

Was this helpful?