1

问题总结

  • 我有两个视图控制器(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)
      }
    }
  }

 }
4

1 回答 1

1

问题是,当您创建VC1并传递selfasmvcDelegate时,您实际上传递VC2的是即将被解雇的,并且在解雇之后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 是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
于 2020-09-14T16:48:05.943 回答