ios - UIViewController加载和释放策略

标签 ios swift uiviewcontroller sprite-kit

我正在基于下述导航逻辑使用 SpriteKit 和 Swift 构建游戏。


(来源:noelshack.com)

下面的代码说明了从一个屏幕转到另一个屏幕的逻辑(这里是从启动屏幕转到首选项设置屏幕或级别选择屏幕)

class StartupScreen: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        let StartView = self.view as SKView
        let StartScene = StartSceneClass(size: self.view.bounds.size)
        StartView.ignoresSiblingOrder = true
        StartScene.scaleMode = .AspectFill
        
        StartView.presentScene(StartScene)
    }

+ some other code

}

class StartSceneClass: SKScene
{

+ some init and game logic code…

override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
    {
        for touch: AnyObject in touches
        {
            let location = touch.locationInNode(self)
            
            switch self.nodeAtPoint(location)
            {
            case self.playButton:                
                let vc = self.view?.window?.rootViewController?.storyboard!.instantiateViewControllerWithIdentifier("ChoixLevelVC") as ChoixLevelVC
                self.view?.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)
                
            case self.BoutonPrefs:
                let vc = self.view?.window?.rootViewController?.storyboard!.instantiateViewControllerWithIdentifier("PlayLevelVC") as PlayLevelVC
                self.view?.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)

            default:
                break
            }
        }
    }
}

我使用与上述相同的逻辑从 Level Choice Screen 转到 Play Level Screen。

我遇到的问题是:

  • 当我从关卡选择屏幕转到播放关卡屏幕时,我 在过渡期间查看下面的启动屏幕
  • 当我退出播放级别屏幕时,我直接返回到启动 屏幕而不是我来自的级别选择屏幕
  • 我第二次从 Level Choice Screen 转到 Play Level 屏幕,我找回了上次的所有 Sprite 。 SK场景 当我关闭时,播放关卡屏幕的 UIViewController。

关于最后一个问题,我想要的是每次退出时完全杀死/删除播放关卡屏幕。

到目前为止,我已经在 SKScene 或被调用的 UIViewController 的 UIViewController 中实现了 self.view?.window?.rootViewController?.dismissViewControllerAnimated(false, completion: nil) 以关闭 View Controller .

我已经阅读了很多关于关闭 UIViewController 的方法的资料,并且我知道推荐的方法是从调用 ViewController 中执行此操作(在我的例子中,这意味着我应该从 Level 中关闭 Play Level Screen选择屏幕)。但是,我找不到这种方式的 Swift 示例(很多我无法理解的 Obj-C 示例)。

我想尽可能少地使用 Storyboard,并以编程方式完成大部分任务。今天我只是使用 Storyboard来创建每个 ViewController 并加载它,因为我还找不到一种方法来对所有内容进行编码。

感谢您提供有关正确实现此类导航逻辑的最佳方式的任何链接或建议。

最佳答案

当我从 Level Choice Screen 转到 Play Level Screen 时,我在过渡期间看到了下面的 Startup Screen

在我看来,这就像您推送到的 UIViewController 具有清晰的背景,在这种情况下,您仍然能够看到底层 VC。

当我退出播放级别屏幕时,我直接返回到启动屏幕,而不是我来自的级别选择屏幕

您使用的是 popViewController 还是 popToRootViewController?使用 popViewController 时,它应该按预期工作。

我第二次从 Level Choice Screen 转到 Play Level Screen 时,我找回了上一次的所有 Sprite 。当我关闭 UIViewController 时,Play Level Screen 的 SKScene 没有被删除。

如果我错了请纠正我,但根据我的理解,当你弹出一个 UIViewController 时,它会在堆上/内存中停留一段时间,直到另一个 UIViewController 被推送到 UINavigationController。

您可以尝试清空 viewWillDisappear 中的 Sprite 吗?

关于ios - UIViewController加载和释放策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28905454/

相关文章:

ios - swift search Bar和navigation bar启动后隐藏

ios - Storyboard的子类 ViewController

c# - Xamarin iOS 11.2 中的 Environment.TickCount 替代方案

swift - Swift 中 Lazy var 和 var as-a-closure 的区别

ios - Swift 中自动重新加载 map

ios - 使用 Swift (iOS) 创建可点击的 body 图

Xcode 4.3 : how to create a nib linked to a view controller

ios - 从后台进入前台时无法显示admob插页式广告

ios - 无法清除UIWebView缓存

ios - 前端后端通信 iOS 应用程序