Don't try to present UIViewController
from SKScene
directly, this breaks MVC pattern. SKScene
is part of View
, View
should not know anything about ViewController
.
Instead, you can use NSNotificationCenter
to notify SKScene
's UIViewController
that it should present another UIViewController
:
In SKScene
's UIViewController
:
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(goToGameOverViewController:)
name:@"GoToGameOverViewController"
object:nil];
}
.
-(void)goToGameOverViewController:(NSNotification *) notification {
// Perform a segue or present ViewController directly
//[self performSegueWithIdentifier:@"GameOverSegue" sender:self];
HelpViewController *helpVC = [[HelpViewController alloc]initWithNibName:@"HelpViewController" bundle:nil];
[self presentViewController:helpVC animated: YES completion:nil];
}
.
- (void) dealloc
{
// If you don't remove yourself as an observer, the Notification Center
// will continue to try and send notification objects to the deallocated
// object.
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
In SKScene
:
- (void)gameOver {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"GoToGameOverViewController" object:self];
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…