2015-11-07 114 views
0

我是一個新的程序員,我有2個視圖控制器,當我的遊戲結束後,彈出一個「再次播放」按鈕。返回主視圖控制器?

而不是在第二個視圖控制器上的計時器後重新啓動遊戲我希望該遊戲再次返回到具有播放按鈕的第一個視圖控制器。我該怎麼辦?我有以下警報代碼:

func subtractTime() { 
    seconds-- 
    timerLabel.text = "Time: \(seconds)" 

    if(seconds == 0) { 

     timer.invalidate() 
     let alert = UIAlertController(title: "Time is up!", 
      message: "You scored \(count) points", 
      preferredStyle: UIAlertControllerStyle.Alert) 
     alert.addAction(UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler: { 
      action in self.setupGame() 
     })) 
     presentViewController(alert, animated: true, completion:nil) 


    } 
} 

回答

0

它可以通過賽格瑞告訴故事板連接到另一個視圖控制器在指定的動作稱爲

對於SEGUE被通常做,你將需要首先通過聲明它的標識符來設置故事板中的segue。然後在你的功能,你可以presentViewController

let mainViewController = yourMainControllerName(); 
presentViewController(mainViewController, animated: true, completion: nil) 

後立即撥打

performSegueWithIdentifier("segueId", sender: nil) 

我相信你也能做到現在,你應該將其中一方在處理用戶在警報解除後。 addAction

alert.addAction(action in self.setupGame() UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler:{(ViewController: UIViewController!) in let mainViewController: secondViewController = segue.mainViewController as secondViewController} 
相關問題