如何将以编程方式创建的视图控制器的视图设置为 SKView? [英] How to set view of programatically created view controller to SKView?

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

问题描述

我正在开发一款游戏,并希望有一个 sprite-kit 场景有自己的视图控制器(我经过多次辩论得出的结论),并希望避免使用故事板(我可以完成我想要的故事板,但想学习如何在没有故事板的情况下做到这一点).

I am developing a game and would like to have one sprite-kit scene have its own view controller (a conclusion I've reached after much debate), and would like to avoid using storyboard (I can accomplish what I want with storyboard, but would like to learn how to do it without storyboard).

在我的主视图控制器中,单击按钮后,我有以下代码

In my main view controller, after clicking a button, I have the following code

MyViewController2 *test = [[MyViewController2 alloc] init]; 
test.view = [[SKView alloc] init];
[self presentViewController: test animated:YES completion:nil];

然而,这只是转换一个没有任何内容的灰色屏幕(还没有覆盖-(id)init").如果我覆盖 MyViewController2 中的-(id)init"方法,则会调用以下代码:

However, this just transitions a grey screen with nothing on it (did not overwrite "-(id)init" yet). The following code gets called if I overwrite the "-(id)init" method in MyViewController2:

-(id)init
{
    NSLog(@"init overwrite works");
    SKView * skView = (SKView *)self.view;
    SKScene2 *scene = [SKScene2 sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeFill;
    [skView presentScene:scene];
    return self;
}

但它不会显示,因为我收到此错误:

but it won't display because I get this error:

-[UIView presentScene:]: unrecognized selector sent to instance 

我知道这个错误意味着什么(presentScene 只能由 skView 执行,不能由 UIView 执行),但是即使当我创建 ViewController 并在它的初始化中我将视图设置为仅 SKView,我仍然得到这个错误.我怎样才能解决这个问题?感谢您的时间和帮助.

I know what this error means (the presentScene can only be executed by skView and can't be executed by UIView), but even though when I create the ViewController and in its init I set views to be only SKView, I still get this error. How can I fix this? Thanks for your time and help.

推荐答案

view 属性由 UIViewController 管理,代表根视图.您不能在任何时候简单地指定自己的视图.

The view property is managed by the UIViewController and represents the root view. You cannot simply assign your own view at any point.

选项 1

如果你想用你自己的视图替换视图属性,你可以覆盖 UIViewControllerloadView 方法并在那里分配视图.它记录在此方法的类参考中:

If you wish to replace the view property with your own view you can override UIViewController's loadView method and assign the view there. It is documented in the class reference for this method:

视图控制器在其视图属性为请求,但目前为零.此方法加载或创建一个视图并将其分配给视图属性

The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property

...

您可以重写此方法以手动创建视图.如果您选择这样做,请将视图层次结构的根视图分配给视图属性

You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view property

所以它看起来像这样:

- (void)loadView 
{
    self.view = [[SKView alloc] init];
}

选项 2

您可以将 SKView 添加为子视图.

You can add your SKView as a subview.

向您的视图控制器添加一个属性,以便您可以在必要时访问它:

Add a property to your view controller so you can access it when necessary:

@property (strong, nonatomic) SKView *skView;

在视图加载时初始化并添加为子视图:

Initialize and add it as a subview when the view is loaded:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.skView = [[SKView alloc] init];
    [self.view addSubview:self.skView];

    SKScene2 *scene = [SKScene2 sceneWithSize:self.skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeFill;
    [self.skView presentScene:scene];
}

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

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