如何关闭当前 ViewController 并转到 Swift 中的另一个视图
How to dismiss the current ViewController and go to another View in 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()
}
在 GameViewController 中,我使用了这段代码,但它并没有关闭 GameViewController。
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("GameOverView") as! GameOverView
self.presentViewController(nextViewController, animated:true, completion:nil)
这是我的 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
}
}
有什么建议吗?
你可以做的是让 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)
}
}
当你想关闭GameOverView
时需要调用上面的代码。
希望对你有所帮助。
下面的代码将带您进入主要部分 VC,这是一段经过反复测试的代码。
self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
我是 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()
}
在 GameViewController 中,我使用了这段代码,但它并没有关闭 GameViewController。
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("GameOverView") as! GameOverView
self.presentViewController(nextViewController, animated:true, completion:nil)
这是我的 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
}
}
有什么建议吗?
你可以做的是让 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)
}
}
当你想关闭GameOverView
时需要调用上面的代码。
希望对你有所帮助。
下面的代码将带您进入主要部分 VC,这是一段经过反复测试的代码。
self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)