如何在不仅使用 Storyboard 的情况下以编程方式导航 Objective-C 应用程序? [英] How To Navigate Objective-C App Programmatically Without Using Only Storyboard?

查看:24
本文介绍了如何在不仅使用 Storyboard 的情况下以编程方式导航 Objective-C 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在浏览应用程序时遇到问题.我已经使用Objective-C完全在Sprite Kit(所有按钮等,使用SKNodes)中编写了一个游戏场景.当用户打开应用程序时,它会显示一个主菜单,该菜单完全构建在 Storyboard 中,并包含一个 UIButton,它触发到 ViewController 的转换,其中包含我的游戏场景.

I am having trouble navigating my application. I have written a game scene completely in Sprite Kit (all buttons, etc. using SKNodes) with Objective-C. When the user opens the app, it presents a main menu which is built completely in Storyboard and contains a UIButton that triggers a transition to the ViewController with my game scene in it.

最初,为了在用户丢失后重新启动游戏,我只是呈现了这样的新场景:

Originally, to restart the game once the user lost I just presented a new scene like this:

GameScene *newScene = [GameScene sceneWithSize:self.view.bounds.size];
newScene.anchorPoint = CGPointMake(0.5, 0.5);
[self.view presentScene:newScene transition:[SKTransition fadeWithDuration:1]];
[self removeAllChildren];
[newScene sceneSetupWithSize:screenSize];

其中 sceneSetupWithSize:(CGSize)size 是我用来设置游戏的自定义方法.不幸的是,无论我发送什么值,这个新场景总是具有相同的错误大小.此过程也无法让我导航回主菜单.

Where sceneSetupWithSize:(CGSize)size is a custom method I am using to set up my game. Unfortunately, this new scene always has the same incorrect size no matter what value I send. This procedure also does not enable me to navigate back to the main menu.

为了解决这个问题,我决定只调用场景的 ViewController.我通过在游戏场景类中创建一个 ViewController 对象来做到这一点:

To fix this, I resolved to just call the scene's ViewController. I did this by creating a ViewController object in the game scene class:

@property (nonatomic) GameViewController *currentController;

然后在 ViewController 加载场景时分配控制器:

And then assigning the controller when the ViewController is loading the scene:

sceneNode.currentController = self;

然后我使用该对象从游戏场景类中调用 ViewController 中的自定义方法,如下所示:

I then use that object to call a custom method in the ViewController from the game scene class like this:

// In Game Scene Class
[self removeAllChildren];
[self removeAllActions];
[_currentController restart];

// In ViewController Class
- (void)restart{
     NSLog(@"Restarting Scene");

     NSString * storyboardName = @"Main";
     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
     UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"GameView"];
     vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
     [self presentViewController:vc animated:YES completion:nil];
}

这还允许我通过更改标识符导航到主菜单.除了一个问题之外,它就像一个魅力:它永远不会从堆栈中弹出旧的 ViewControllers.因此,在运行应用程序时,每次我导航到下一个 ViewController 时,它都会向处理内存增加约 40MB.这意味着每次用户输掉并重新启动游戏时,都会占用越来越多的内存.我试图用它来代替:

This also allows me to navigate to the main menu just by changing the identifier. It works like a charm except for one problem: it never pops the old ViewControllers from the stack. So when running the app, every time I navigate to the next ViewController it adds ~40MB to the processing memory. This means that every time the user loses and restarts the game, it takes up more and more memory. I attempted to use this instead:

[self.navigationController pushViewController:vc animated:YES];

但它只是给了我一个空白屏幕.之后我什至尝试像这样弹出当前控制器:

But it just gives me a blank screen. I even tried to pop the current controller afterwards like this:

[self.navigationController popViewControllerAnimated:YES];

但是没有任何效果.我假设 self.navigationControllernil,但我不知道如何处理它.我不想在游戏场景中使用任何 UIButton.我也更喜欢按原样使用游戏场景,而无需在 Storyboard 中重新创建它.

But nothing worked. I'm assuming that self.navigationController is nil, but I don't know what to do with that. I would prefer not to use any UIButtons in the game scene. I would also prefer to use the game scene as is without needing to recreate it in Storyboard.

我知道那里有类似的问题,我已经阅读了大部分问题,但没有一个对我有帮助.

I understand that there are similar questions out there, I have read most of them but none have helped me.

感谢大家的帮助.

推荐答案

这让我花了很长时间才意识到.我一直在尝试做的只是更改根视图.这段代码解决了我的问题:

This took me way too long to realise. What I have been trying to do is simply change the root view. This code solves my problem:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"GameView"];
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
UIApplication.sharedApplication.delegate.window.rootViewController = vc;

这篇关于如何在不仅使用 Storyboard 的情况下以编程方式导航 Objective-C 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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