min*_*ity 38 viewcontroller dismiss ios swift
我正在使用SpriteKit制作游戏.我有3个viewControllers:选择level vc,game vc和win vc.游戏结束后,我想显示win vc,然后如果我按下win vc上的OK按钮,我想解雇win vc和游戏vc(从堆栈中弹出两个视图控制器).但我不知道怎么做,因为如果我打电话
self.dismissViewControllerAnimated(true, completion: {})
Run Code Online (Sandbox Code Playgroud)
win vc(堆栈顶部)被解雇,所以我不知道在哪里再次调用它来解雇游戏vc.有没有办法在不使用导航控制器的情况下解决这个问题?
这是第一个VC :(请注意以下以"//"开头的评论)
class SelectLevelViewController: UIViewController { // I implemented a UIButton on its storyboard, and its segue shows GameViewController
override func viewDidLoad() {
super.viewDidLoad()
}
}
Run Code Online (Sandbox Code Playgroud)
这是第二个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() {...}
}
Run Code Online (Sandbox Code Playgroud)
这是第三个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()
}
}
Run Code Online (Sandbox Code Playgroud)
Phl*_*man 108
@Ken Toh的评论是在这种情况下对我有用的 - 从视图控制器调用你想要在其他一切被解雇后显示的消除.
如果你有3个呈现视图控制器"栈" A,B和C,其中,C是在上面,然后调用A.dismiss(animated: true, completion: nil)将同时解雇B和C.
如果您没有对堆栈根目录的引用,则可以链接几个访问权限presentingViewController以获取它.像这样的东西:
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
Ken*_*Toh 28
您可以在完成块中关闭WinVC的呈现控制器(GameViewController):
let presentingViewController = self.presentingViewController
self.dismissViewControllerAnimated(false, completion: {
presentingViewController?.dismissViewControllerAnimated(true, completion: {})
})
Run Code Online (Sandbox Code Playgroud)
或者,您可以联系到根视图控制器并调用dismissViewControllerAnimated,这将在单个动画中关闭两个模态视图控制器:
self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: {})
Run Code Online (Sandbox Code Playgroud)
Raf*_*ael 11
Swift 5(可能还有 4、3 等)
presentingViewController?.presentingViewController?不是很优雅,在某些情况下不起作用。相反,使用segues.
比方说,我们有ViewControllerA,ViewControllerB和ViewControllerC。我们在ViewControllerC(我们通过ViewControllerA->到达这里ViewControllerB,所以如果我们这样做,dismiss我们将返回到ViewControllerB)。我们想从ViewControllerC直接跳回到ViewControllerA。
在ViewControllerA您的 ViewController 类中添加以下操作:
@IBAction func unwindToViewControllerA(segue: UIStoryboardSegue) {}
Run Code Online (Sandbox Code Playgroud)
是的,这一行在你想要返回的 ViewController 的 ViewController 中!
现在,您需要从ViewControllerC的故事板 ( StoryboardC)创建退出转场。继续并打开StoryboardC并选择故事板。按住 CTRL 并拖动退出,如下所示:
您将获得一个 segue 列表供您选择,包括我们刚刚创建的一个:
你现在应该有一个 segue,点击它:
在ViewControllerC您想要关闭并返回的点ViewControllerA,执行以下操作(使用我们之前在检查器中设置的 id):
self.performSegue(withIdentifier: "yourIdHere", sender: self)
Run Code Online (Sandbox Code Playgroud)
你应该可以打电话:
self.presentingViewController.dismissViewControllerAnimated(true, completion: {});
Run Code Online (Sandbox Code Playgroud)
(您可能需要添加?或在!某处 - 我不是一个快速的开发人员)
在我的应用程序中尝试接受的答案时,我遇到了一些动画问题。先前呈现的视图将在屏幕上闪烁或尝试动画。这是我的解决方案:
if let first = presentingViewController,
let second = first.presentingViewController,
let third = second.presentingViewController {
second.view.isHidden = true
first.view.isHidden = true
third.dismiss(animated: true)
}
Run Code Online (Sandbox Code Playgroud)