所以我想通了,主要来自这篇文章 – http://makeapppie.com/2014/09/15/swift-swift-programmatic-navigation-view-controllers-in-swift/
在SecondViewController的类声明上方,添加以下代码:
protocol SecondVCDelegate {
func didFinishSecondVC(controller: SecondViewController)
}
然后在SecondViewContoller里面添加一个类变量:
var delegate: MeditationVCDelegate! = nil
然后在按钮所针对的函数内部,添加以下内容:
self.navigationController?.popViewControllerAnimated(true)
delegate.didFinishSecondVC(self)
我们在这里所做的是在SecondViewController 中进行弹出,并且不传递任何数据,但是由于我们已经定义了一个协议,我们将在@987654328 中使用它@ 处理数据。
接下来,在ViewController 中,将您在SecondViewController 中定义的协议添加到ViewController 继承自的类列表中:
class ViewController: UIViewController, SecondVCDelegate { ... your code... }
您需要添加我们在新协议中定义的函数,以使编译器满意。在ViewController 的类中,添加:
func didFinishSecondVC(controller: SecondViewController) {
self.myBoolVar = true
controller.navigationController?.popViewControllerAnimated(true)
}
在我们调用didFinishSecondVC 的SecondViewController 中,我们在ViewController 类内部调用这个方法,我们要弹出的控制器。这类似于我们在 SecondViewController 内编写此代码,但我们已将其编写在 ViewController 内,并且我们使用委托来管理两者之间的消息传递。
最后,在ViewController 中,在我们要push 到SecondViewController 的函数中,添加以下代码:
let secondVC = secondViewController()
secondVC.delegate = self
self.navigationController?.pushViewController(secondVC, animated: true)
就是这样!你应该准备好在两个视图控制器之间传递代码而不使用故事板!