关闭一个 View Controller 并以相同的方法呈现一个新的 - swift

Dismiss a View Controller and present a new one in the same method - swift

问题总结

我试过的

我遇到过类似的问题:

我用的是最新的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)
      }
    }
  }

 }

问题是,当您创建 VC1 并将 self 作为 mvcDelegate 传递时,您实际上传递了 VC2,它即将被解散,并且在dismiss VC2 无法呈现任何视图控制器。

您可能需要在呈现之前将一个视图控制器的委托传递给另一个:

// Pass your delegate to the other view controller delegate
mvc.mvcDelegate = rvcDelegate
// dismiss ResultViewController
dismiss(animated: true) {
    // present MainViewController by using instance of VcDelegate protocol
    self.rvcDelegate?.presentVc(vc: mvc)
}

反之亦然:

// Pass your delegate to the other view controller delegate
rvc.rvcDelegate = mvcDelegate
// dismiss MainViewController
dismiss(animated: true) {
    // present ResultViewController by using instance of VcDelegate protocol
    self.mvcDelegate?.presentVc(vc: rvc)
}

正如评论中提到的 VC1 是应用程序 window 的 rootViewController。所以,要实现你正在寻找的东西,你需要替换 rootViewController.

在 MainViewController 中:

// Change rootViewController of the view's window with ResultViewController
view.window?.rootViewController = rvc

在 ResultViewController 中也一样:

// Change rootViewController of the view's window with MainViewController
view.window?.rootViewController = mvc