If you want to press a button from one_view_controller and stop the sound on another view controller, follow these steps:
Follow these steps:
Step 1:
Add this code to your controller from which you want to press a button to stop the sound.
@IBAction func btnStopSound(_ sender: AnyObject) { NSNotificationCenter.defaultCenter().postNotificationName("stopSoundNotification", object: nil) }
Swift 4 Version:
@IBAction func btnStopSound(_ sender: AnyObject) { NotificationCenter.default.post(name: Notification.Name(rawValue: "setTimerValue"), object: nil) }
Step 2:
Now is his last step. Now add this code below to the results controller where you want to automatically stop the sound.
func loadList(notification: NSNotification){ metronomePlay.metronome.stop() } override func viewWillAppear(animated: Bool) { NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:",name:"stopSoundNotification", object: nil) }
Swift 4 Version:
func loadList(notification: Notification){ metronomePlay.metronome.stop() } override func viewWillAppear(animated: Bool) { NotificationCenter.default.addObserver(self, selector: Selector(("loadList:")), name: Notification.Name(rawValue: "stopSoundNotification"), object: nil) }
Now implement this code in your own encoding. Hope this works for you.
source share