Need notification in code, if user change View

I am using an AVSpeechSythesizer(), which reads the list (TableViewCells) on one view to the user, if the user hits a button. If the user hits the button again the AVSpeechSynthesizer() stops, as I want it to be. But I want the AVSpeechSynthesizer() to stop as well, if the user change the view (go back or hit the home Button). Is it possible that the ViewController of that view get Notification, if the View is no longer shown or have I to check in the other ViewControllers?

Answered by OOPer in 664740022
Maybe you can use viewWillDisappear.
Accepted Answer
Maybe you can use viewWillDisappear.
Accepted Answer
What you should do, in the VC :

Declare at class level:
Code Block
    var synthesizer : AVSpeechSynthesizer?


Everywhere you need to speak, you set the var : // Avoid superposition of speakers
Code Block
if synthesizer != nil {
synthesizer!.stopSpeaking(at: .immediate)
synthesizer = nil
}
let newSynthe = whatToSpeak.speakIt()
synthesizer = newSynthe

And close when disappear:
Code Block
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if synthesizer != nil {
synthesizer!.stopSpeaking(at: .immediate)
synthesizer = nil
}
}


Used, works well.
Thank you, worked well!
Need notification in code, if user change View
 
 
Q