How To Navigate Objective-C App Programmatically W

2019-08-26 21:10发布

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];

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.

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;

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

sceneNode.currentController = self;

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];
}

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];

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.

Thank you all for any help.

1条回答
可以哭但决不认输i
2楼-- · 2019-08-26 21:46

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;
查看更多
登录 后发表回答