ios 如何关闭当前的 ViewController 并在 Swift 中转到另一个视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31952948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dismiss the current ViewController and go to another View in Swift
提问by Michel Kansou
I am new to Swift and I want to know how to dismiss the current view controller and go to another view.
我是 Swift 的新手,我想知道如何关闭当前的视图控制器并转到另一个视图。
My storyboard is like the following: MainMenuView -> GameViewController -> GameOverView. I want to dismiss the GameViewController to go to the GameOverView, not to the MainMenuView.
我的故事板如下所示:MainMenuView -> GameViewController -> GameOverView。我想关闭 GameViewController 以转到 GameOverView,而不是 MainMenuView。
I use the following code in my 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()
}
In the GameViewController, I use this code, but it doesn't dismiss the GameViewController.
在 GameViewController 中,我使用了此代码,但它不会关闭 GameViewController。
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("GameOverView") as! GameOverView
self.presentViewController(nextViewController, animated:true, completion:nil)
This is My GameOverView Code :
这是我的 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
}
}
Any suggestions?
有什么建议?
回答by Victor Sigler
What you can do is let the GameOverView
be presented, after all when you presenting it the GameViewController
is below in the hierarchy, and then in your GameOverView
run the following code to close both when you want to dismiss the GameOverView
, like in the following way:
您可以做的是让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)
}
}
The above code need to be called when you want to dismiss the GameOverView
.
当您想关闭GameOverView
.
I hope this help you.
我希望这对你有帮助。
回答by Abhinav Jha
The below code will take you to the main VC, Here's a tried and tested piece of code.
下面的代码将带您进入主 VC,这是一段经过试验和测试的代码。
self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)