Group Group Group Group Group Group Group Group Group

Create function to call switch in another class

#1

I want to create a code that calls a class that checks to see if the switch is on or off. So if class a and b call class sound. The function in class a and b checks sounds switch if the switch is on it plays sound if its off it will not play the sound. But for the purpose of this lets just change the background color by switch. It will be less code.

                         class a: UIViewController {

    func call(){}
  override func viewDidLoad() {
super.viewDidLoad()
   call()
     }
}

class b: UIViewController {


      func call(){}
    override func viewDidLoad() {
super.viewDidLoad()
   call()
}
}



    class sound: UIViewController {


     var switchy: UISwitch()
   override func viewDidLoad() {
super.viewDidLoad()

}
#2

Hi Tim,

I really want to help you, but I don’t understand your question. Would you please adjust it a little? Also, it might help if you would change your function name to something other than “call”, because I really can’t distinguish whether you mean the class function “call()” or to call the class method.

#3

Think of it like this i have several classes the each play their unique sound. I would like to create a settings class that has a switch that control weather each class has the audio on of off. Its like a master audio switch for all of the classes.

#4

A master switch for all classes would be simply:

class SoundManager {
    // Anybody can *read* this, only SoundManager can change it
    private (set) var audioOn = true
}

class A: UIViewController {
    func maybePlaySound() {
        guard soundManager.audioOn else { return }
        // Now play the sound
        ...
    }
}

… with maybe some UI to wire a toggle switch up to change the value of audioOn ?

If you want SoundManager to have an on-off switch per class, that’s going to be a little more involved.

#5

thanks for your support.