Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

ios - How do I go from SKScene to UIViewController by code?

All I want if for when the user touches a skspritenode in the skscene, it will go to a different view like performseguewithidentifier. Thanks for any help. I can post code, but it seems like a generic question so I figured that you wouldn't need any. By the way, I already figured out how to detect the tapping the skspritenode. I have been looking at this for a long time and I am stumped. Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You cannot present a viewController from within a SKScene as it is actually only being rendered on a SKView. You need a way to send a message to the SKView's viewController, which in turn will present the viewController. For this, you can use delegation or NSNotificationCenter.

Delegation

Add the following protocol definition to your SKScene's .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 point where you want to present the share screen, use this line:

[self.delegate showDifferentView];

Now, in your viewController's .h file, implement the protocol:

@interface ViewController : UIViewController <sceneDelegate>

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

scene.delegate = self;

Then add the following method there:

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

NSNotificationCenter

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

Add the viewController as a listener to the notification in it's -viewDidLoad method:

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

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

[[NSNotificationCenter defaultCenter] postNotificationName:@"showDifferentView" object:nil];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...