Swift View Controller 不会关闭

标签 swift xcode uiview

所以我在 Xcode 中有一个基本的游戏。我从 Xcode 的基本游戏模板开始,主要在“GameScene.swift”中构建我的游戏。当我完成我的游戏时,我想添加一个带有播放按钮的开始屏幕,所以我制作了一个新的 UIViewController 并添加了一个按钮。我控制单击并将按钮拖动到 GameViewController 并创建了一个模态 Segue。然后我希望游戏在玩家死亡时返回。我尝试了各种方法来驳斥这种观点,但都没有奏效。我是 swift 的新手,真的需要帮助。让我知道找到解决方案需要哪些代码/信息。我已经尝试了我在互联网上找到的所有内容。我以为我找到了一种解决方法,方法是在菜单的 GameViewController 中添加一个按钮和 Segue,但在多次按下后,它阻塞了系统并减慢了爬行速度,因为没有一个被解雇。我可以提供任何需要的代码。

在 GameViewController 类的 GameViewController.swift 内部,我尝试创建一个在游戏结束时调用的函数。 我试图同时弹出和关闭 View Controller 。调用了该函数并向控制台打印了一行,但 View 仍然存在。唯一打印到控制台的是 'nil'

class GameViewController: UIViewController {
   func end(){
     print(navigationController?.viewControllers as Any)
     self.navigationController?.popViewController(animated: true)
     self.dismiss(animated: true, completion: nil)
   }

@ Vollan heres the pic

Here is the screenshot of the storyboard .

提前致谢。

最佳答案

如果你推送 viewController,你使用 self.navigationController?.popViewController(animated: true)

如果您以模态方式呈现它,您可以使用 self.dismiss(self, animated: true)

当它从您使用的模态转场中呈现时 self.presentingViewController?.dismiss(animated: true, completion: nil)

关于Swift View Controller 不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49725705/

相关文章:

ios - 为什么app在设置属性时会陷入死循环?

ios - 一旦我在STPAddCardViewController()上选择完成,应用程序就会加载并且不会停止加载

ios - 如何设置自定义 UIView 属性

ios - UIView 和 UIButton 的动画区别

ios - 替代基于非图像内容的 UIView 的 contentStretch

Swift:如何检查子类的基类和父类(super class)

swift - NSJSONSerialization 在 Playground 中未按预期工作

swift - 如何在 Swift 中使用 Xcode Logger

objective-c - 将一个数组中的所有元素与另一个数组进行比较

objective-c - 客观c类(class)的简单问题