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?
Need notification in code, if user change View
Maybe you can use viewWillDisappear.
What you should do, in the VC :
Declare at class level:
Everywhere you need to speak, you set the var : // Avoid superposition of speakers
And close when disappear:
Used, works well.
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!