【问题标题】:How to close a View Controller after Performing segue in Xcode?在 Xcode 中执行 segue 后如何关闭视图控制器?
【发布时间】:2020-11-10 05:46:55
【问题描述】:

我的故事板看起来像这样:-

Main View Controller -> Game View Controller -> Game Result View Controller

我执行了从 Main V.C 到 Game V.C 的模态转场。现在,当我从 Game V.C. 执行模态转场时。游戏结果 V.C.,游戏 V.C.没有关闭,因为游戏 V.C.检测是否开始触摸,当它检测到 Game V.C. 之外的触摸时会崩溃。在对 Game Result V.C. 执行模态转场之后我没有使用导航视图控制器。任何人都可以帮助我在对 Game Result V.C. 执行模态 segue 后如何关闭 Game V.C.?感谢您的帮助!谢谢:)

**Game V.C.** 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  // First touch to start the game
  if gameState == .ready {
    startGame()
  }
  
  if let touchLocation = event?.allTouches?.first?.location(in: self.view) {
    // Move the player to the new position
    movePlayer(to: touchLocation)
    
    // Move all enemies to the new position to trace the player
    moveEnemies(to: touchLocation)
  }

}

func gameOver() {
    stopGame()
    performSegue(withIdentifier: "2to3segue", sender: self)
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    一些提示:

    方法一

    您可以使用 Tap 控制器,让情节提要如下所示

    主视图控制器 -> (tab-1) 游戏视图控制器 \ -> (tab-2) 游戏结果视图控制器

    Hide tab bar,按程序换tab。

    我建议使用method 1

    方法二

    关闭Game View Controller没有动画。然后呈现Game Result View ControllerWITH动画。

    但是,我记得method 2 有一些问题。之前用过,现在终于用method 1了。

     // Example:
     // Two or more animations will produce problem.
     v2.dismiss(animated: false, completion: nil);
     v.present(v3, animated: true, completion: nil);
    

    【讨论】:

    • 感谢您的回答!您能否提供任何其他解决方案而不是使用标签栏控制器?谢谢:)
    • 嗯,如果你不想使用标签栏控制器,你可以试试Method 2。但我记得Method 2 有一些用户界面问题。
    【解决方案2】:
    First dismiss the "v2" view controller and in completion block present "v3" view controller
    
    v2.dismiss(animated: false, completion: {
      v.present(v3, animated: true, completion: nil)
    })
    
    or
    
    if v2 is your presented view controller then :
    
    self.presentedViewController.dismiss(animated: false, completion: {
         v.present(v3, animated: true, completion: nil)
     })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-17
      • 2017-09-08
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多