Mic*_*sou 15 viewcontroller ios segue swift
我是Swift的新手,我想知道如何关闭当前的视图控制器并转到另一个视图.
我的故事板如下所示:MainMenuView - > GameViewController - > GameOverView.我想解雇GameViewController转到GameOverView,而不是MainMenuView.
我在MainMenuView中使用以下代码:
@IBAction func StartButton(sender: UIButton) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("GameViewController") as! GameViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
restGame()
}
Run Code Online (Sandbox Code Playgroud)
在GameViewController中,我使用此代码,但它不会忽略GameViewController.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("GameOverView") as! GameOverView
self.presentViewController(nextViewController, animated:true, completion:nil)
Run Code Online (Sandbox Code Playgroud)
这是我的GameOverView代码:
class GameOverView: UIViewController{
// save the presenting ViewController
var presentingViewController :UIViewController! = self.presentViewController
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func ReplayButton(sender: UIButton) {
restGame()
didPressClose()
}
@IBAction func ReturnMainMenu(sender: UIButton) {
Data.GameStarted = 1
self.dismissViewControllerAnimated(false) {
// go back to MainMenuView as the eyes of the user
self.presentingViewController.dismissViewControllerAnimated(false, completion: nil);
}
/* let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("MainScene") as! MainScene
self.presentViewController(nextViewController, animated:true, completion:nil)*/
}
func restGame(){
Data.score = 0
Data.GameHolder = 3
Data.GameStarted = 1
Data.PlayerLife = 3.0
Data.BonusHolder = 30
Data.BonusTimer = 0
}
func didPressClose()
{
self.self.dismissViewControllerAnimated(true, completion:nil)
}
override func shouldAutorotate() -> Bool {
return false
}
deinit{
print("GameOverView is being deInitialized.");
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
Vic*_*ler 40
你可以做的就是让它GameOverView呈现出来,毕竟当你呈现它时,它GameViewController位于层次结构的下方,然后在你的GameOverView运行中运行以下代码,当你想要解雇时,关闭它们GameOverView,如下面的方式:
@IBAction func ReturnMainMenu(sender: UIButton) {
// save the presenting ViewController
var presentingViewController: UIViewController! = self.presentingViewController
self.dismissViewControllerAnimated(false) {
// go back to MainMenuView as the eyes of the user
presentingViewController.dismissViewControllerAnimated(false, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
当你想要解雇时,需要调用上面的代码GameOverView.
我希望这对你有帮助.
| 归档时间: |
|
| 查看次数: |
41034 次 |
| 最近记录: |