How to go from SKScene to UIViewController by code?

All I want is if for a user regarding skspritenode in skscene, he will switch to another view, for example, performseguewithidentifier . Thanks for any help. I can post the code, but this seems like a general question, so I decided you wouldn't need it. By the way, I already figured out how to detect skspritenode clicks. I looked at this for a long time, and I'm at a standstill. Please, help.

+7
ios uiviewcontroller sprite-kit segue skscene
source share
3 answers

You cannot imagine the viewController from SKScene, since it is actually displayed only in SKView. You need to send a message to the SKView viewController, which in turn will present the viewController. You can use delegation or NSNotificationCenter for this.

Delegation

Add the following protocol definition to your SKScene.h file:

 @protocol sceneDelegate <NSObject> -(void)showDifferentView; @end 

And declare a delegate property in the interface:

 @property (weak, nonatomic) id <sceneDelegate> delegate; 

Then, at the moment you want to present a common screen, use this line:

 [self.delegate showDifferentView]; 

Now, in your viewController.h file, execute the protocol:

 @interface ViewController : UIViewController <sceneDelegate> 

And in your .m file, add the following line before presenting the scene:

 scene.delegate = self; 

Then add the following method:

 -(void)showDifferentView { [self performSegueWithIdentifier:@"whateverIdentifier"]; } 

NSNotificationCenter

Save the -showDifferentView method as described in the previous alternative.

Add viewController as a listener for notification in it -viewDidLoad:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showDifferentView) name:@"showDifferenView" object:nil]; 

Then in the scene at the point where you want to display this viewController, use this line:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"showDifferentView" object:nil]; 
+9
source share

I suggest the following option: (this happens in your scene)

 -(void)presentViewController{ MyViewController *myController = [[MyViewController alloc]init]; //Or instantiate any controller from the storyboard with an indentifier [self.view.window.rootViewController presentViewController:myController animated: YES options: nil]; } 

And later in your view, the controller, when you want to fire it, do something like this:

 -(void)dismissItself:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; } 

The good thing about this option is that you will not need to save your view, since at any time you can initialize it while you are in, import the viewcontroller code.

Let me know if it works.

+2
source share

ZeMoon, a nice answer, but what if several scenes want to show the same view controller, for example, the settings screen? You do not want to define multiple delegate scene protocols that do the same.

Another idea is to define one protocol that handles the presentation of various view controllers for you:

 @protocol ScreenFlowController <NSObject> - (void)presentSettingsScreenFromScene:(SKScene *)scene; - (void)presentCreditsScreenFromScene:(SKScene *)scene; @end 

The scene parameter can be used to make decisions based on where you came from.

The view controller of your game (or any other object) implements the protocol. All scenes displaying another screen receive a weak link to the controller:

 @property (nonatomic, weak) id<ScreenFlowController> screenFlowController; 
0
source share

All Articles