简体   繁体   English

如何使用swift关闭和解除(游戏)ViewController?

[英]How to both dismiss and deinit a (Game)ViewController using swift?

What I want to achieve: segue from gameVC to mainmenuVC and get rid of the gameVC 我想要实现的目标:从gameVC到mainmenuVC,摆脱gameVC

When the app starts it first shows a main menu viewcontroller with a play button that segues to the gameviewcontroller. 当应用程序启动时,它首先会显示一个主菜单viewcontroller,其中包含一个播放按钮,该按钮会分段到gameviewcontroller。 When the user taps on a menu button sprite the following function in the gameviewcontroller gets called and it segues back to the main menu: 当用户点击菜单按钮时,将调用gameviewcontroller中的以下函数并将其切换回主菜单:

func returnToMainMenu () {

    //This works but does not deinit the vc
    navController?.dismissViewControllerAnimated(true, completion: nil)

    /* this does not do anything:
    navController?.popViewControllerAnimated(true)
    */

}

This is probably not how it is done properly and I think that might be the problem, but I could not get to work otherwise because gameViewController.navigationViewController is nil. 这可能不是它如何正确完成,我认为这可能是问题,但我无法开始工作,因为gameViewController.navigationViewController是零。

This is how my storyboard looks: 这就是我的故事板的样子: 故事板 This is how the memory usage looks when the app is running. 这是应用程序运行时内存使用情况的样子。 Those spikes/steps occur whenever the gameviewcontroller is loaded. 每当加载gameviewcontroller时都会出现这些尖峰/步骤。 It seems to me that the problem is, that the gameviewcontroller does not de-initialize when returnToMainMenu() is called. 在我看来,问题是,当调用returnToMainMenu()时,gameviewcontroller不会去初始化。

app运行时的内存使用情况 Also, this never gets executed: 此外,这永远不会执行:

 deinit {
    debugPrintln("GameViewController deinitialized")
}

update: 更新:

I deleted this 我删除了这个

navController = self

and defined navController in returnToMainMenu like this: 并在returnToMainMenu中定义了navController,如下所示:

let navController = view.window?.rootViewController as! UINavigationController

segue back to main menu still works but it still does not deinit the vc segue回到主菜单仍然有效,但它仍然没有取消vc

Of course it will memory leak. 当然它会记忆泄漏。

override func viewDidLoad() {
    navController = self
}

You just gave yourself a reference to itself. 你刚给自己一个参考。 Usually when your vc goes offscreen, the view hierarchy no longer holds the view so the view is deinited. 通常当您的vc离开屏幕时,视图层次结构不再保留视图,因此视图会被取消。 You set a reference to itself so whatever you do, it will always hold itself in memory and will never deinit. 你设置了一个对自己的引用,所以无论你做什么,它总会把它自己留在记忆中,永远不会消失。

I figured it out after watching Lecture 8 of the stanford iOS8 course (at 14:23). 我在观看了斯坦福iOS8课程的第8讲(14:23)后想出来了。

The problem was that I added a reference to the gameviewcontroller in my gamescene to call its returnToMainMenu() method from the scene. 问题是我在我的gamescene中添加了对gameviewcontroller的引用,以从场景调用它的returnToMainMenu()方法。 In order for its memory to be cleared all the references to the VC have to be set to nil. 为了清除其内存,必须将对VC的所有引用设置为nil。

I solved it by referring to the navigation controller directly from my scene like this: 我通过直接从我的场景中引用导航控制器来解决它,如下所示:

(scene!.view!.window?.rootViewController as! UINavigationController).dismissViewControllerAnimated(false, completion: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM