我正在使用SpriteKit制作游戏。我有3个viewControllers:选择level vc,game vc和win vc。游戏结束后,我想显示win vc,然后如果我按下win vc上的OK按钮,我想解雇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的评论是在这种情况下对我有用的 - 在视图控制器中调用您想要在其他所有内容被解除后显示的消除。
如果你有3个呈现视图控制器A
,B
和C
的“堆叠”,其中C
位于顶部,那么调用A.dismiss(animated: true, completion: nil)
将同时解除B和C.
如果你没有对堆栈根的引用,你可以链接几个访问presentingViewController
来访问它。像这样的东西:
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
您可以在完成块中关闭WinVC的呈现控制器(GameViewController):
let presentingViewController = self.presentingViewController
self.dismissViewControllerAnimated(false, completion: {
presentingViewController?.dismissViewControllerAnimated(true, completion: {})
})
或者,您可以联系到根视图控制器并调用dismissViewControllerAnimated,这将在单个动画中关闭两个模态视图控制器:
self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: {})
你应该可以打电话:
self.presentingViewController.dismissViewControllerAnimated(true, completion: {});
(您可能需要在某处添加?
或!
- 我不是一个快速的开发人员)
有一个特殊的展开segue旨在将视图堆栈回滚到某个视图控制器。请在这里查看我的答案:how to dismiss 2 view controller in swift ios?
在我的应用程序中尝试接受的答案时,我遇到了一些动画问题。之前显示的视图将闪烁或尝试在屏幕上设置动画。这是我的解决方案:
if let first = presentingViewController,
let second = first.presentingViewController,
let third = second.presentingViewController {
second.view.isHidden = true
first.view.isHidden = true
third.dismiss(animated: true)
}
Swift 4.0
let presentingViewController = self.presentingViewController
presentingViewController?.presentingViewController?.presentingViewController?.dismiss(animated: false, completion: nil)
在打电话时加入Phlippie Bosman的答案
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
如果你不想看(presentingViewController
会是什么),你可以做类似的事情
self.presentingViewController?.view.addSubview(self.view)
这看起来有点hacky,但到目前为止,这是我能够让它看起来像两个视图控制器同时解雇的唯一方法。
Swift 5(可能是4,3等)
presentingViewController?.presentingViewController?
不是很优雅,在某些情况下不起作用。相反,使用segues
。
假设我们有ViewControllerA
,ViewControllerB
和ViewControllerC
。我们在ViewControllerC
(我们通过ViewControllerA
登陆 - > ViewControllerB
,所以如果我们做dismiss
,我们将回到ViewControllerB
)。我们希望从ViewControllerC
直接回到ViewControllerA
。
在ViewControllerA
中,在ViewController类中添加以下操作:
@IBAction func unwindToViewControllerA(segue: UIStoryboardSegue) {}
是的,这一行放在你想要回到的ViewController的ViewController中!
现在,您需要从ViewControllerC
的故事板(StoryboardC
)创建一个退出segue。继续打开StoryboardC
并选择故事板。按住CTRL并拖动以退出,如下所示:
您将获得一个可供选择的segue列表,包括我们刚刚创建的segue:
你现在应该有一个segue,点击它:
在ViewControllerC
中您想要解雇并返回ViewControllerA
,请执行以下操作(使用我们之前在检查器中设置的ID):
self.performSegue(withIdentifier: "yourIdHere", sender: self)
虽然Rafeels的回答是可以接受的。不是每个人都使用Segue的。
对我来说,以下解决方案效果最佳
if let viewControllers = self.navigationController?.viewControllers {
let viewControllerArray = viewControllers.filter {
$0 is CustomAViewController || $0 is CustomBViewController }
DispatchQueue.main.async {
self.navigationController?.setViewControllers(viewControllerArray,
animated: true)
}
}