问题
I've come to the point where I need to make a start screen for my game made with sprite kit. I can not get it to show with any of the code and tutorials I have read and watched.
I want to use a navigation controller/view controller to begin at the start screen and have it linked to my app but I am not sure how to link my game's code to the view controller.
Using a navigation controller, how would I drag it into my game's code as a @IBAction declaration? Thank you.
Edit with current code:
ViewController.m:
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
scene.viewController = self;
[skView presentScene:scene];
}
MyScene.h:
@interface MyScene : SKScene <SKPhysicsContactDelegate>
@property (strong, nonatomic) UIViewController *viewController;
@end
回答1:
This answer is based on the one here, but has been translated to Objective-C.
The problem that you are facing is that an SKScene does not have direct access to it's viewController, but just the view in which it is contained. You need to create a pointer to it manually. This can be done by creating a property for the SKScene:
@interface GameScene : SKScene
@property (strong, nonatomic) UIViewController *viewController;
@end
Then, in the viewController class, just before [skView presentScene:scene];
scene.viewController = self;
Now, you can access the viewController directly. Simply call the segue on this viewController:
-(void)goToHomeScreen {
[self.viewController.navigationController popToRootViewControllerAnimated:YES];
}
You can perform other navigationController operations in a similar way.
来源:https://stackoverflow.com/questions/27096991/using-navigation-controller-for-a-start-screen-ios