您可以創建一個委託是這樣的:
protocol StopAnimationDelegate{
func stopAnimations()
}
那麼,你的第一個視圖控制器上你要採取這個協議:
class FirstViewController : UIViewController, StopAnimationDelegate{
//..... here code
func stopAnimations(){
//Stop your animations or call your method stopAni here.
}
//.... here more code
@IBAction func openSecondViewController(sender:UIButton){
self.performSegueWithIdentifier("segue_first_second",sender:nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segue_first_second"{
let secondViewController = segue.destinationViewController as! SecondViewController
secondViewController.delegate = self
}
}
}
關於第二個視圖控制器,你可以做類似:
class SecondViewController: UIViewController{
var delegate:StopAnimationDelegate?
@override func viewDidLoad(){
delegate?.stopAnimations()
}
}
注:這是一個如何實現這一目標的一種方式,但所有德依賴於你需要做的事情,例如你可以簡單地停止動畫,當你執行segue(但是,這又取決於你想要做什麼)。
另一個選項,使用NSNotificationCenter來發布通知,停止播放動畫,是這樣的:
在第一個視圖控制器:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "stopAnim", name: "kStopAnimations", object: nil)
}
//...Your stopAnim method
//... More Code
}
class SecondViewController : UIViewController{
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().postNotificationName("kStopAnimations", object: nil)
}
}
我以前建議的SWIFT代碼由你,但它是不工作! @JoséRoberto Abreu – John
@John你可以上傳一個示例項目來檢查嗎? –