如何在不仅使用情节提要的情况下以编程方式导航 Objective-C 应用程序?
How To Navigate Objective-C App Programmatically Without Using Only Storyboard?
我在浏览我的应用程序时遇到问题。我已经用 Objective-C 完全在 Sprite Kit 中编写了一个游戏场景(所有按钮等都使用 SKNodes)。当用户打开应用程序时,它会显示一个主菜单,该菜单完全构建在 Storyboard 中,并包含一个 UIButton,可触发向 ViewController 的过渡,其中包含我的游戏场景。
本来为了在用户输了后重新开始游戏,我只是呈现了一个这样的新场景:
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
是我用来设置游戏的自定义方法。不幸的是,无论我发送什么值,这个新场景总是具有相同的错误大小。此过程也无法让我导航回主菜单。
为了解决这个问题,我决定只调用场景的 ViewController。我通过在游戏场景 class:
中创建一个 ViewController 对象来做到这一点
@property (nonatomic) GameViewController *currentController;
然后在 ViewController 加载场景时分配控制器:
sceneNode.currentController = self;
然后我使用该对象从游戏场景 class 中调用 ViewController 中的自定义方法,如下所示:
// 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。这意味着每次用户输了再重新开始游戏,占用的内存就会越来越多。我试图改用它:
[self.navigationController pushViewController:vc animated:YES];
但它只是给我一个空白屏幕。之后我什至尝试像这样弹出当前控制器:
[self.navigationController popViewControllerAnimated:YES];
但没有任何效果。我假设 self.navigationController
是 nil
,但我不知道该怎么做。我不想在游戏场景中使用任何 UIButtons。我也更愿意按原样使用游戏场景,而无需在 Storyboard 中重新创建它。
我知道那里有类似的问题,我已经阅读了其中的大部分内容,但 none 对我有所帮助。
感谢大家的帮助。
我花了太长时间才意识到这一点。我一直在尝试做的只是更改根视图。此代码解决了我的问题:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"GameView"];
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
UIApplication.sharedApplication.delegate.window.rootViewController = vc;
我在浏览我的应用程序时遇到问题。我已经用 Objective-C 完全在 Sprite Kit 中编写了一个游戏场景(所有按钮等都使用 SKNodes)。当用户打开应用程序时,它会显示一个主菜单,该菜单完全构建在 Storyboard 中,并包含一个 UIButton,可触发向 ViewController 的过渡,其中包含我的游戏场景。
本来为了在用户输了后重新开始游戏,我只是呈现了一个这样的新场景:
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
是我用来设置游戏的自定义方法。不幸的是,无论我发送什么值,这个新场景总是具有相同的错误大小。此过程也无法让我导航回主菜单。
为了解决这个问题,我决定只调用场景的 ViewController。我通过在游戏场景 class:
中创建一个 ViewController 对象来做到这一点@property (nonatomic) GameViewController *currentController;
然后在 ViewController 加载场景时分配控制器:
sceneNode.currentController = self;
然后我使用该对象从游戏场景 class 中调用 ViewController 中的自定义方法,如下所示:
// 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。这意味着每次用户输了再重新开始游戏,占用的内存就会越来越多。我试图改用它:
[self.navigationController pushViewController:vc animated:YES];
但它只是给我一个空白屏幕。之后我什至尝试像这样弹出当前控制器:
[self.navigationController popViewControllerAnimated:YES];
但没有任何效果。我假设 self.navigationController
是 nil
,但我不知道该怎么做。我不想在游戏场景中使用任何 UIButtons。我也更愿意按原样使用游戏场景,而无需在 Storyboard 中重新创建它。
我知道那里有类似的问题,我已经阅读了其中的大部分内容,但 none 对我有所帮助。
感谢大家的帮助。
我花了太长时间才意识到这一点。我一直在尝试做的只是更改根视图。此代码解决了我的问题:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"GameView"];
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
UIApplication.sharedApplication.delegate.window.rootViewController = vc;