Calling function from another ViewController in swift Calling function from another ViewController in swift ios ios

Calling function from another ViewController in swift


As of swift 4.1 today, this code worked for me:

Put this in sending controller:

NotificationCenter.default.post(name: Notification.Name(rawValue: "disconnectPaxiSockets"), object: nil)

Put this in receiving controller viewDidLoad() or viewWillAppear():

NotificationCenter.default.addObserver(self, selector: #selector(disconnectPaxiSocket(_:)), name: Notification.Name(rawValue: "disconnectPaxiSockets"), object: nil)

and then the following function in your receiving controller class:

@objc func disconnectPaxiSocket(_ notification: Notification) {    ridesTimer.invalidate()    shared.disconnectSockets(socket: self.socket)}


Swift 5:

Put this in the Action

NotificationCenter.default.post(name: Notification.Name("NewFunctionName"), object: nil)

Put this in viewdidload() in a different viewcontroller (where is the function you want to use)

NotificationCenter.default.addObserver(self, selector: #selector(functionName), name: Notification.Name("NewFunctionName"), object: nil)

The function

 @objc func functionName (notification: NSNotification){ //add stuff here}

I hope I was helpful


You are creating a NEW copy of FirstVC and calling stop on something that is not yet initialised.

You should really use a delegate in this case, something like

protocol controlsAudio {   func startAudio()   func stopAudio()}class FirstVC: UIViewController, controlsAudio {    func startAudio() {}    func stopAudio() {}    // later in the code when you present SecondVC    func displaySecondVC() {       let vc = SecondVC()       vc.delegate = self       self.present(vc, animated: true)    }}class SecondVC: UIViewController {    var delegate: controlsAudio?    // to start audio call self.delegate?.startAudio)    // to stop audio call self.delegate?.stopAudio)}

So you are passing first VC to the second VC, so when you call these functions you are doing it on the actual FirstVC that is in use, rather than creating a new one.

You could do this without protocols if you like by replacing the var delegate: controlsAudio? with var firstVC: FirstVC? and assigning that, but I wouldn't recommend it