问题总结
我有两个视图控制器(VC1 = MainViewController 和 VC2 = ResultViewController)。在 VC1 中有一个名为 endGame() 的方法。在这种方法中,我想同时关闭 VC1 并呈现 VC2。
在 VC2 中有一个按钮函数叫做 playAgainButton()。在这个函数中,我想关闭 VC2 并呈现 VC1。
当我尝试先关闭 VC1 然后呈现 VC2 时,VC1 无法呈现 VC2,因为 VC1 已经被关闭并且不存在于堆栈中。
dismiss(animated: true) { self.present(rvc, animated: true, completion: nil) }
当我尝试先呈现 VC2 然后关闭 VC1 时,VC2 会出现 1 秒钟然后立即消失。
present(rvc, animated: true) { self.dismiss(animated: true, completion: nil) }
我试过的
我想出了类似的问题:
- 关闭一个 View Controller 并显示另一个 View Controller。这对我的问题有最接近的答案。它建议使用协议。因此,通过使用协议,我解决了一半的问题 --> 在 VC2 中,我既可以解除 VC2,也可以呈现 VC1。但是在 VC1 中,我不能同时关闭 VC1 并呈现 VC2。
我使用最新的 swift 和 Xcode 版本。
我的代码
VC1
// protocol for presenting View Controllers
protocol VcDelegate: AnyObject {
func presentVc(vc: UIViewController)
}
// The main view controller of the game.
class MainViewController: UIViewController, UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout, UICollectionViewDelegate,
UIGestureRecognizerDelegate, VcDelegate {
// MARK: - Protocol functions & properties
func presentVc(vc: UIViewController) {
present(vc, animated: false, completion: nil)
}
// instance of VcDelegate protocol
weak var mvcDelegate: VcDelegate?
// MARK: - Properties
// the level of the game
var level: Int = 0
// the player's score
var score: Int = 0
/// Terminate the game for the next level and show results page
private func gameOver() {
// present the resultViewController
if let rvc = storyboard?.instantiateViewController(withIdentifier: "ResultViewController") as? ResultViewController {
rvc.resultLevel = level
rvc.resultScore = score
rvc.rvcDelegate = self
// dismiss MainViewController
dismiss(animated: true) {
// present ResultViewController by using instance of VcDelegate protocol
self.mvcDelegate?.presentVc(vc: rvc)
}
}
}
}
VC2
// This page is viewed after a level is finished. It shows level results and play again button.
class ResultViewController: UIViewController, VcDelegate {
// MARK: - Protocol functions & properties
func presentVc(vc: UIViewController) {
present(vc, animated: false, completion: nil)
}
weak var rvcDelegate: VcDelegate?
// MARK: - Properties
// variable showing game level
var resultLevel = 0
// variable showing current score
var resultScore = 0
/// When play again button is tapped a new game starts (a new mainViewController is presented)
@IBAction func playAgainButton(_ sender: UIButton) {
// present a new MainViewController
if let mvc = storyboard?.instantiateViewController(withIdentifier: "MainViewController") as? MainViewController {
mvc.level = resultLevel
mvc.score = resultScore
mvc.mvcDelegate = self
// dismiss ResultViewController
dismiss(animated: true) {
// present MainViewController by using instance of VcDelegate protocol
self.rvcDelegate?.presentVc(vc: mvc)
}
}
}
}