簡體   English   中英

在 Xcode 中執行 segue 后如何關閉視圖控制器?

[英]How to close a View Controller after Performing segue in Xcode?

我的故事板看起來像這樣:-

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

我已經執行了從 Main VC 到 Game VC 的模態轉場現在當我執行從 Game VC 到 Game Result VC 的模態轉場時,Game VC 沒有關閉,並且由於 Game VC 檢測到是否開始觸摸,它在檢測到觸摸時崩潰在對 Game Result VC 執行模式 segue 之后,在 Game VC 之外我沒有使用導航視圖控制器。 任何人都可以幫助我在對 Game Result VC 執行模式切換后如何關閉 Game VC? 感謝您的幫助! 謝謝:)

**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)
}

一些技巧:

方法一

您可以使用 Tap 控制器,讓故事板工作如下

Main View Controller -> (tab-1) Game View Controller 
                            \ 
                             -> (tab-2) Game Result View Controller

隱藏標簽欄,並按程序更改標簽。

我建議使用method 1

方法二

關閉Game View Controller沒有動畫。 然后呈現帶有動畫的Game Result View Controller

但是,我記得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);
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)
 })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM