我正在使用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()
}
}
答案 0 :(得分:87)
如果您有3个呈现的视图控制器“{堆叠”A
,B
和C
,其中C
位于顶部,则调用A.dismiss(animated: true, completion: nil)
将同时解雇B和C.
如果您没有对堆栈根目录的引用,则可以对presentingViewController
进行几次访问以获取它。像这样:
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
答案 1 :(得分:27)
您可以在完成块中解除WinVC的呈现控制器(GameViewController):
let presentingViewController = self.presentingViewController
self.dismissViewControllerAnimated(false, completion: {
presentingViewController?.dismissViewControllerAnimated(true, completion: {})
})
或者,您可以联系到根视图控制器并调用dismissViewControllerAnimated,这将在单个动画中关闭两个模态视图控件:
self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: {})
答案 2 :(得分:7)
您应该可以致电:
self.presentingViewController.dismissViewControllerAnimated(true, completion: {});
(您可能需要在某处添加?
或!
- 我不是一个快速的开发者)
答案 3 :(得分:5)
用于将视图堆栈回滚到某个视图控制器的特殊展开segue 。请在此处查看我的回答:how to dismiss 2 view controller in swift ios?
答案 4 :(得分:2)
在我的应用程序中尝试接受的答案时,我遇到了一些动画问题。先前显示的视图将闪烁或尝试在屏幕上制作动画。这是我的解决方案:
if let first = presentingViewController,
let second = first.presentingViewController,
let third = second.presentingViewController {
second.view.isHidden = true
first.view.isHidden = true
third.dismiss(animated: true)
}
答案 5 :(得分:0)
Swift 4.0
let presentingViewController = self.presentingViewController
presentingViewController?.presentingViewController?.presentingViewController?.dismiss(animated: false, completion: nil)
答案 6 :(得分:0)
在致电
时添加到Phlippie Bosman的答案self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
如果您不希望看到presentingViewController
,可以执行
self.presentingViewController?.view.addSubview(self.view)
这似乎有点棘手,但是到目前为止,这是我能够使两个视图控制器一致关闭的唯一方法。
答案 7 :(得分:0)
快速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
)中创建退出脚本。继续并打开StoryboardC
并选择情节提要。按住CTRL键并拖动以退出,如下所示:
系统会为您提供一系列的选择,包括我们刚刚创建的选择:
您现在应该拥有一个segue,单击它:
在ViewControllerC
上您要撤消并返回到ViewControllerA
的位置,执行以下操作(使用我们先前在检查器中设置的ID):
self.performSegue(withIdentifier: "yourIdHere", sender: self)
答案 8 :(得分:0)
尽管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)
}
}