以编程方式创建ViewController时,如何将其视图设置为SKVIew类型? [英] When programatically creating a ViewController, how can I set its view to be of type SKVIew?

查看:457
本文介绍了以编程方式创建ViewController时,如何将其视图设置为SKVIew类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XCode在iOS上开发游戏。我有几个SKScenes,每个都与UIViewController类相关联。因此,在每个视图控制器的viewDidLoad中,我创建

I am developing a game in iOS on XCode. I have several SKScenes each associated with a UIViewController class. So in each of the view controller's viewDidLoad, I create

SKView *skView = (SKView *)self.view
SKScene * scene = [whateverSKScene sceneWithSize:skView.bounds.size]
[skView present scene]



<我想避开故事板。但是,当我从第一个视图控制器向下一个视图控制器提示ViewController时,应用程序在[skView当前场景]行崩溃,因为它认为新视图控制器的视图仍然是UIView(' - [UIView presentScene:]:无法识别的选择器已发送例如)。

I want to avoid the storyboard. However, when I presentViewController from the first view controller to the next, the app crashes at the line "[skView present scene]" because it thinks the new view controller's view is still UIView ('-[UIView presentScene:]: unrecognized selector sent to instance).

我该如何解决这个问题?具体来说,在创建第二个视图控制器(以编程方式)时,如何将其视图更改为SKview类型而不是UIview?
(我应该注意到我希望视频控制器的场景是1:1,因为几乎所有这些都是弹出窗口,比如弹出游戏或弹出设置的暂停菜单等)

How can I fix this? Specifically, when creating the 2nd view controller (programatically), how can I change its view to be of type SKview not UIview? (I should note I want the scenes to be 1:1 with view controllers because almost all of them are popups like a pause-menu pop up over game play or settings pop up etc)

推荐答案

尝试在自定义ViewController上实现loadView方法,如下所示:

Try implementing the loadView method on your custom ViewController, like so:

override func loadView() {
        self.view = SKView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
}

这是Apple的文件所述:

This is what Apple's documentation states:


如果
他们没有使用nib,那么子类应该创建自定义视图层次结构。永远不应该直接调用。

This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.

这篇关于以编程方式创建ViewController时,如何将其视图设置为SKVIew类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆