我正在使用SpriteKit制作游戏。我有3个viewControllers:选择级别vc,游戏vc和win vc。游戏结束后,我想显示Win vc,然后如果我按Win vc上的“确定”按钮,我想关闭Win vc和游戏vc(将两个视图控制器弹出堆栈)。但我不知道该怎么办,因为如果我打电话
self.dismissViewControllerAnimated(true, completion: {})
Win vc(堆栈顶部)被关闭了,所以我不知道在哪里再次调用它来关闭游戏vc。有什么方法可以在不使用导航控制器的情况下解决此问题?
这是第一个VC :(请注意以下以“ //”开头的评论)
class SelectLevelViewController: UIViewController { // I implemented a UIButton on its storyboard, and its segue shows GameViewController override func viewDidLoad() { super.viewDidLoad() } }
这是第二个VC:
class GameViewController: UIViewController, UIPopoverPresentationControllerDelegate { var scene: GameScene! var stage: Stage! var startTime = NSTimeInterval() var timer = NSTimer() var seconds: Double = 0 var timeStopped = false var score = 0 @IBOutlet weak var targetLabel: UILabel! @IBOutlet var displayTimeLabel: UILabel! @IBOutlet weak var scoreLabel: UILabel! @IBOutlet weak var gameOverPanel: UIImageView! @IBOutlet weak var shuffleButton: UIButton! @IBOutlet weak var msNum: UILabel! var mapNum = Int() var stageNum = Int() var tapGestureRecognizer: UITapGestureRecognizer! override func viewDidLoad() { super.viewDidLoad() let skView = view as! SKView skView.multipleTouchEnabled = false scene = GameScene(size: skView.bounds.size) scene.scaleMode = .AspectFill msNum.text = "\(mapNum) - \(stageNum)" stage = Stage(filename: "Map_0_Stage_1") scene.stage = stage scene.addTiles() scene.swipeHandler = handleSwipe gameOverPanel.hidden = true shuffleButton.hidden = true skView.presentScene(scene) Sound.backgroundMusic.play() beginGame() } func beginGame() { displayTimeLabel.text = String(format: "%ld", stage.maximumTime) score = 0 updateLabels() stage.resetComboMultiplier() scene.animateBeginGame() { self.shuffleButton.hidden = false } shuffle() startTiming() } func showWin() { gameOverPanel.hidden = false scene.userInteractionEnabled = false shuffleButton.hidden = true scene.animateGameOver() { self.tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "hideWin") self.view.addGestureRecognizer(self.tapGestureRecognizer) } } func hideWin() { view.removeGestureRecognizer(tapGestureRecognizer) tapGestureRecognizer = nil gameOverPanel.hidden = true scene.userInteractionEnabled = true self.performSegueWithIdentifier("win", sender: self) // this segue shows WinVC but idk where to dismiss this GameVC after WinVC gets dismissed... } func shuffle() {...} func startTiming() {...} }
这是第三个VC:
class WinVC: UIViewController { @IBOutlet weak var awardResult: UILabel! @IBAction func dismissVC(sender: UIButton) { self.dismissViewControllerAnimated(true, completion: {}) // dismissing WinVC here when this button is clicked } override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
@Ken Toh的评论在这种情况下对我有用-在关闭所有其他内容后,从视图控制器中调用您想显示的dismiss。
如果你有3个呈现视图控制器“栈” A,B和C,其中,C是在上面,然后调用A.dismiss(animated: true, completion: nil)将同时解雇B和C.
A
B
C
A.dismiss(animated: true, completion: nil)
如果您没有对堆栈根的引用,则可以将几个访问链接在一起以到达堆栈presentingViewController。像这样:
presentingViewController
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)