0

I have a "ViewControllerA" [VCA] and a view that slides into view when swiped (A hamburger menu?). It's ViewController is "ViewControllerB"[VCB]

I use this library to implement the side drawer https://github.com/dekatotoro/SlideMenuControllerSwift

What I want to achieve is that upon clicking on the button on VCB (after it slides onto VCA) the theme of the pages change. So I want update the colours of multiple elements.

How do I change the colours of elements in VCA without dismissing/reloading either controllers?

Some screenshots Blue background is VCA

2 Answers 2

1

The simplest way to achieve your desired result you can go with NotificationCenter

Add an observer to the ViewController where you would like to get the event of theme changed (in your case it will be A)

NotificationCenter.default.addObserver(self, selector: #selector(self.themeChanged), name: "your_notification_name", object: nil)

Add a method to handle the event of a post notification

@objc func self.themeChanged(notification: NSNotification){ //Your code }

Post the notification once you taped on the theme change button from another ViewController (in your case it will be B)

NotificationCenter.default.post(name: Notification.Name("your_notification_name"), object: OBJ_TO_BE_SEND)

IMP : Please don't forget to remove the observer if it keeps adding

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. I have a question What would be the OBJ_TO_BE_SEND in the object parameter?
OBJ_TO_BE_SEND is the sender of the notification. you can find more about NotificationCenter here... learnappmaking.com/notification-center-how-to-swift
-2

This framework is making use of embedded view controllers. What you need to do is add a property called VCA of type ViewControllerA to ViewControllerB's class:

class ViewControllerB: UIViewController {
    var VCA: ViewControllerA!
}

Then, in ViewControllerA, you need to implement prepare(for segue:) so that, when ViewControllerB is initialized, you set its VCA property so that ViewControllerA is stored as ViewControllerB's VCA.

class ViewControllerA: UIViewController {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let VCB = segue.destination as? ViewControllerB {
            VCB.VCA = self
        }
    }
}

Then, whenever clicking the theme button on ViewControllerB, you need to reference VCA and change its properties:

class ViewControllerB: UIViewController {
    var VCA: ViewControllerA!

    @IBAction func themeButtonPressed(_ sender: Any?) {
        if UserDefaults.standard.bool(forKey: "DarkThemeOn") {
            //Set the background color to light for both the menu VC and VCA
            view.backgroundColor = .white
            VCA.view.backgroundColor = .white
        } else {
            //Set the background color to dark for both the menu VC and VCA
            view.backgroundColor = .black
            VCA.view.backgroundColor = .black
        }

        //Toggle the theme
        UserDefaults.standard.set(!UserDefaults.standard.bool(forKey: "DarkThemeOn"), forKey: "DarkThemeOn")
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.