So, I understood, based mainly on this post - http://makeapppie.com/2014/09/15/swift-swift-programmatic-navigation-view-controllers-in-swift/
In the SecondViewController , above the class declaration, add this code:
protocol SecondVCDelegate { func didFinishSecondVC(controller: SecondViewController) }
Then inside the SecondViewContoller add a class variable:
var delegate: MeditationVCDelegate! = nil
Then, inside your function that your button is aiming at, add the following:
self.navigationController?.popViewControllerAnimated(true) delegate.didFinishSecondVC(self)
What we do here does pop in the SecondViewController and does not pass any data, but since we defined the protocol, we will use this in the ViewController to process the data.
So, in the ViewController add the protocol that you defined in SecondViewController to the SecondViewController 's list of classes inherited from:
class ViewController: UIViewController, SecondVCDelegate { ... your code... }
You need to add the function defined in the new protocol to make the compiler happy. Inside the ViewController class ViewController add the following:
func didFinishSecondVC(controller: SecondViewController) { self.myBoolVar = true controller.navigationController?.popViewControllerAnimated(true) }
In SecondViewController , where we call didFinishSecondVC , we call this method inside the ViewController class, to the controller that we are accessing. It is like we wrote this code inside the SecondViewController , but we wrote it inside the ViewController , and we use the delegate to control the messaging between them.
Finally, in the ViewController in the function that we aim to click on SecondViewController , add this code:
let secondVC = secondViewController() secondVC.delegate = self self.navigationController?.pushViewController(secondVC, animated: true)
What is it! You must be set up to pass code between two view controllers without using a storyboard!